Odi's astoundingly incomplete notes
New entriesCode
back | nextslow pasting in vim
I often copy text files via SSH by just opening the target file in vim and then pasting the text into the terminal.
So what's going on here? vim is writing to its swap file. Character by character. Dutifully flushing each character out of the buffers. Very good in case you lose power, except that it really slows us down here...
Simple solution: disable vim's swap file with the -n option.
vim -n targetfile
- Ctrl-A Ctrl-C
- ssh targethost
- vim targetfile
- gg (top of file)
- dG (delete to end of file)
- :set paste (no auto indent)
- i (insert)
- Shfit-Ctrl-V
So what's going on here? vim is writing to its swap file. Character by character. Dutifully flushing each character out of the buffers. Very good in case you lose power, except that it really slows us down here...
Simple solution: disable vim's swap file with the -n option.
vim -n targetfile
Add comment
Java's OutputStreamWriter can waste memory
The following little unit test measures the memory usage when you convert from char to byte using an OutputStreamWriter. It does that once without buffering and once with a BufferedWriter wrapped around the stream.
The result is surprising:
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import junit.framework.TestCase; public class StreamTest extends TestCase { public void test1() throws Exception { Runtime rt = Runtime.getRuntime(); final int max = 10000; File f = File.createTempFile("junit", "tmp"); int gcs = gcs(); long used = rt.totalMemory() - rt.freeMemory(); Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); for (int i=0; i<max; i++) { w.write("constant"); } w.close(); assertEquals("increase -Xms -Xmx", gcs, gcs()); long used2 = rt.totalMemory() - rt.freeMemory(); System.out.println("unbuffered: "+ (used2 - used) +" bytes"); f.delete(); gcs = gcs(); used = rt.totalMemory() - rt.freeMemory(); w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8")); for (int i=0; i<max; i++) { w.write("constant"); } w.close(); assertEquals("increase -Xms -Xmx", gcs, gcs()); used2 = rt.totalMemory() - rt.freeMemory(); System.out.println("buffered: "+ (used2 - used) +" bytes"); System.out.println(System.getProperty("java.runtime.version")); } private int gcs() { int sum = 0; for (GarbageCollectorMXBean mb : ManagementFactory.getGarbageCollectorMXBeans()) { sum += mb.getCollectionCount(); } return sum; } }
The result is surprising:
unbuffered: 943896 bytes buffered: 0 bytes 1.7.0_45-b18(The actual numbers depend on the heap size). One would assume that unbuffered writes use no memory, and that buffered writes use the buffer size. However this behaviour suggests that OutputStreamWriter's implementation bears a dirty little secret... And where does the buffer memory go? I can just assume that the VM plays dirty optimization tricks here and quickly allocates the transient buffer from thread-local memory without actually resorting to heap.
Misconceptions about security
Again I am coming across a customer who has severe misconceptions about security. They are:
There is 100% security
That is so wrong, it's not even a good joke anymore. Security and usability always add up to 100%. Something that is 100% secure is totally unusable and will not fulfil any requirements other than security requirements. Want a secure computer? Turn it off.
Security is cheap
No, security is not cheap. Security always adds inconvenience, it always takes extra work, it always causes extra problems, it always makes things more complex, it is always harder to debug.
Hiding something makes it secure
Also known as Security By Obscurity. Just because something is inconvenient to access, doesn't mean it can not be accessed. This category includes: code obfuscation, jump hosts, locally encrypted data(bases) like DRM (key is accessible by the same user), obfuscated passwords.
Security of a client application is of importance
A client can run any code. Whether that is the original application, a modification thereof, a hacked version, a custom implementation or something compeletely different is irrelevant to security. What matters is what you can do over the network protocol. If your protocol is insecure, then security in your client can not help you.
There is 100% security
That is so wrong, it's not even a good joke anymore. Security and usability always add up to 100%. Something that is 100% secure is totally unusable and will not fulfil any requirements other than security requirements. Want a secure computer? Turn it off.
Security is cheap
No, security is not cheap. Security always adds inconvenience, it always takes extra work, it always causes extra problems, it always makes things more complex, it is always harder to debug.
Hiding something makes it secure
Also known as Security By Obscurity. Just because something is inconvenient to access, doesn't mean it can not be accessed. This category includes: code obfuscation, jump hosts, locally encrypted data(bases) like DRM (key is accessible by the same user), obfuscated passwords.
Security of a client application is of importance
A client can run any code. Whether that is the original application, a modification thereof, a hacked version, a custom implementation or something compeletely different is irrelevant to security. What matters is what you can do over the network protocol. If your protocol is insecure, then security in your client can not help you.
sun-jdk-1.6 on Gentoo
Recently dev-java/sun-jdk-1.6 was masked and removed from the
virtual/jdk
ebuild. Of course you can still unmask it (in /etc/portage/package.unmask). But Portage still wants to pull in dev-java/icedtea-bin
. You really don't need that if you have the sun-jdk. To prevent Portage from pulling it in, simply mark a high version of it as provided./etc/portage/profile/package.provided
:
dev-java/icedtea-bin-6.999
Criteria API WTF
From the J2EE tutorial:
Clearly SQL is too simple and boring. You really need a bullshit API to produce a simple SQL query. Of course this will ease maintenance, improve performance, reduce code, prevent bugs, speed up development by a factor of 10 and reduce your project cost by 99% at least. A classic case of overengineering.
EntityManager em = ...; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Root<Pet> pet = cq.from(Pet.class); cq.select(pet); TypedQuery<Pet> q = em.createQuery(cq); List<Pet> allPets = q.getResultList();
The equivalent JPQL query is:
FROM Pet
The equivalent SQL query is:
SELECT * FROM Pet
Clearly SQL is too simple and boring. You really need a bullshit API to produce a simple SQL query. Of course this will ease maintenance, improve performance, reduce code, prevent bugs, speed up development by a factor of 10 and reduce your project cost by 99% at least. A classic case of overengineering.
Yes, finally I found a site complaining about this stupid shit criteria api.
Setting sonar.libraries from an Ant classpath
Sonar Ant task properties use the comma character to separate paths. So a little trick is necessary to integrate that with existing path resources:
<path id="myclasspath"> <pathelement path="lib/mylib.jar"/> </path> <pathconvert property="sonar.libraries" pathsep="," refid="myclasspath"/>
cups-1.6 printer sharing
Gentoo has updated CUPS to version 1.6. If you run a print server you must configure zeroconf correctly, or your clients won't see any remote printers:
The clients need working MDNS too:
and change /etc/nsswitch.conf:
and should run cups-browsed with this config:
/etc/portage/package.use:
net-print/cups dbus zeroconf net-dns/avahi dbus mdnsresponder-compatAnd allow DNS-SD through iptables:
[0:0] -A INPUT -p udp -m udp --dport 5353 -j ACCEPTAlso configure the CUPS browsing protocol:
/etc/cups/cups-browsed.conf
:
BrowseRemoteProtocols none BrowseLocalProtocols DNSSD,CUPSI think CUPS tells avahi about registered printers through D-Bus. Then clients discover registered printers via DNS-SD queries.
The clients need working MDNS too:
emerge -av nss-mdns
and change /etc/nsswitch.conf:
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
and should run cups-browsed with this config:
/etc/cups/cups-browsed.conf
:
BrowseRemoteProtocols DNSSD,CUPS BrowseLocalProtocols none
iptables connection tracking helpers
When your kernel tells you this:
It really urges you to include this in your iptables configuration:
IP_NF_RAW
IP6_NF_RAW
NETFILTER_XT_TARGET_CT
kernel: nf_conntrack: automatic helper assignment is deprecated and it will be removed soon. Use the iptables CT target to attach helpers instead.
It really urges you to include this in your iptables configuration:
*raw :PREROUTING ACCEPT [0:0] :OUTPUT ACCEPT [0:0] [0:0] -A PREROUTING -p tcp --dport 21 -j CT --helper ftp [0:0] -A PREROUTING -p tcp --dport 6667 -j CT --helper irc [0:0] -A PREROUTING -p tcp --dport 6566 -j CT --helper sane COMMITRequired kernel config:
IP_NF_RAW
IP6_NF_RAW
NETFILTER_XT_TARGET_CT
kernel: Package power limit notification
I get this frequently in syslog:
These messages are logged by the kernel, in the file:
This feature is enabled by the X86_MCE_INTEL config option, which is located under:
So the kernel now gets thermal interrupts from your CPU when certain temperature limits are crossed. The kernel's thermal and power management then decides what to do to decrease the temperature. This is normal operation and nothing to worry about.
Dec 28 11:44:00 kernel: CPU0: Package power limit notification (total events = 355) Dec 28 11:44:00 kernel: CPU2: Package power limit notification (total events = 355) Dec 28 11:44:00 kernel: CPU1: Package power limit notification (total events = 355) Dec 28 11:44:00 kernel: CPU3: Package power limit notification (total events = 355) Dec 28 11:44:00 kernel: CPU1: Package power limit normal Dec 28 11:44:00 kernel: CPU0: Package power limit normal Dec 28 11:44:00 kernel: CPU2: Package power limit normal Dec 28 11:44:00 kernel: CPU3: Package power limit normalUsually when Firefox uses a lot of CPU. There are many forum posts regarding these messages. But there is nothing to worry about. A bit of background.
These messages are logged by the kernel, in the file:
arch/x86/kernel/cpu/mcheck/therm_throt.c
This feature is enabled by the X86_MCE_INTEL config option, which is located under:
Processor type and features [*] Machine Check / overheating reporting [*] Intel MCE featuresIn dmesg it shows this message, during boot, if the kernel decided to handle thermal interrupts (and not the BIOS):
CPU0: Thermal monitoring enabled (TM1)
So the kernel now gets thermal interrupts from your CPU when certain temperature limits are crossed. The kernel's thermal and power management then decides what to do to decrease the temperature. This is normal operation and nothing to worry about.
iptables format changes
Gentoo has stabilized iptables 1.4.16. With that some changes to your iptables config are necessary.
Use conntrack instead of state module
before: -m state --state NEW
after: -m conntrack --ctstate NEW
Remove --reap from recent module
before: -m recent --update --seconds 30 --reap -j DROP
after: -m recent --update --seconds 30 -j DROP
Maybe there is more, but I haven't discovered it yet...
back
|
next
Use conntrack instead of state module
before: -m state --state NEW
after: -m conntrack --ctstate NEW
Remove --reap from recent module
before: -m recent --update --seconds 30 --reap -j DROP
after: -m recent --update --seconds 30 -j DROP
Maybe there is more, but I haven't discovered it yet...