Monday, September 15, 2008

Sync clocks using ntp on Linux

I have often faced difficulty in using ntp to sync the clocks of 2 machines. One easy way (i never found it easy though!) is to use a common ntp server on both the machines to sync the clock. But this never worked for me for reasons unknown!

So below is an easy way to synchronize the clock on 2 local machines.

Assume that we have 2 machines - M1 and M2. Now synchronizing the clocks of these 2 machines takes just 3 simple steps!

Step 1: Install ntp on both the machines.
Step 2: Now make M1 as the NTP server which is synced to its local clock.
Step 3: Use 'ntpdate' command to synchronize the clock on M2 machine with M1 machine.

Lets go through these 3 steps in detail:
Step 1: I wont go in much detail about installing ntp. Get the latest package for your OS and install it.

Step 2: To make M1 as local NTP server, you need to have 2 important values in the etc/ntp.conf file. [if ntp.conf file is not present, just create on with following values]

server 127.127.1.0 # Local clock IP
peer 10.10.17.10 # M2 machine IP

You can comment out rest of the lines in the ntp.conf file. At the end of this blog I am attaching a simple ntp.conf file. You can use the same one.

Please note the two values given above. The first line "server 127.127.1.0" represents the local system clock IP. Dont confuse this with 127.0.0.1 - the loopback ip. 127.127.1.0 is the special IP address used to represent local clock.
So with this line we are saying to the ntpd demon that use the local clock itself as the reference.

The next line "peer 10.10.17.10" tells the ntp server that this IP is the peer machine. You need to give M2 IP address here.

With this configuration, now start ntpd demon on M1 machine:

#ntpd -c /etc/ntp.conf

Note that ntpd by default reads the config file from /etc/ntp.conf. In case you want to override that with your own config file, you can do that by using -c option.

Once ntpd is started, you need to wait for around 10-15 seconds for the server the sync its time with the local clock. You can verify this by using "ntpq -p" command, as below:

root@10.10.254.140:~/asp_3.0/etc/init.d# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *LOCAL(0) 73.78.73.84 5 l 62 64 377 0.000 0.000 0.001 10.10.17.10 .INIT. 16 u 18h 1024 0 0.000 0.000 4000.00
Note the above output. Observe the first line where it says "LOCAL(0)" and it has a * next to it. Initially when you start the ntpd you wont see the '*'. You need to wait until the * appears next to the LOCAL(0). - Dont worry about the other values.

Once this is done, your ntp server is UP and ready to be used by other peers.

Step 3: On M2 machine, all you need to do is issue 'ntpdate' command with the M1 IP address:

root@10.10.254.137:~/asp_3.0/etc/init.d# ntpdate 10.10.17.20 2
Jun 16:48:14 ntpdate[2541]: step time server 10.10.17.20 offset 7689.396852 sec

Note that ntpdate detected the offset value and adjusted to the local clock.


Verifying clock difference between 2 machines:

Once you are done with synchronizing the clock between 2 machines, you can verify the clock difference using 'clockdiff' utility.

Just issue 'clockdiff ' on the command line, as shown below:

root@10.10.254.137:~/asp_3.0/etc/init.d# clockdiff 10.10.17.20
..
host=10.10.17.20 rtt=562(280)ms/0ms delta=0ms/0ms Mon Jun 2 17:27:07 2008

If you are running clockdiff on M1, pass the IP address of M2 machine to get the difference.
Note the 'delta' value which shows that the difference is 0. There might be a delta of up to 3-5 milliseconds, which is acceptable.

Installing clockdiff:
clockdiff is part of iputils package. You can download this package from http://www.skbuff.net/iputils/iputils-current.tar.bz2

You need to untar the above file and then do a "make". After that clockdiff binary is created. Just copy this binary to /usr/sbin or /usr/bin.


Did you find this post useful? Please leave a comment! Thanks.

/proc/meminfo Explained

=====

Foreword: I copied the below text from http://www.redhat.com/advice/tips/meminfo.html into my blog as this was a very useful information and I am afraid I may not be able to access it again.

=====

"Free," "buffer," "swap," "dirty." What does it all mean? If you said, "something to do with the Summer of '68", you may need a primer on 'meminfo'.

The entries in the /proc/meminfo can help explain what's going on with your memory usage, if you know how to read it.

Example of "cat /proc/meminfo":

root: total:     used:     free:    shared: buffers: cached:
Mem: 1055760384 1041887232 13873152 0 100417536 711233536
Swap: 1077501952 8540160 1068961792
MemTotal:  1031016 kB 
MemFree: 13548 kB
MemShared: 0 kB
Buffers: 98064 kB
Cached: 692320 kB
SwapCached: 2244 kB
Active: 563112 kB
Inact_dirty: 309584 kB
Inact_clean: 79508 kB
Inact_target: 190440 kB
HighTotal: 130992 kB
HighFree: 1876 kB
LowTotal: 900024 kB
LowFree: 11672 kB
SwapTotal: 1052248 kB
SwapFree: 1043908 kB
Committed_AS: 332340 kB

The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level statistics.

High-Level Statistics

  • MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
  • MemFree: Is sum of LowFree+HighFree (overall stat)
  • MemShared: 0; is here for compat reasons but always zero.
  • Buffers: Memory in buffer cache. mostly useless as metric nowadays
  • Cached: Memory in the pagecache (diskcache) minus SwapCache
  • SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)

Detailed Level Statistics
VM Statistics

VM splits the cache pages into "active" and "inactive" memory. The idea is that if you need memory and some cache needs to be sacrificed for that, you take it from inactive since that's expected to be not used. The vm checks what is used on a regular basis and moves stuff around.

When you use memory, the CPU sets a bit in the pagetable and the VM checks that bit occasionally, and based on that, it can move pages back to active. And within active there's an order of "longest ago not used" (roughly, it's a little more complex in reality). The longest-ago used ones can get moved to inactive. Inactive is split into two in the above kernel (2.4.18-24.8.0). Some have it three.

  • Active: Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
  • Inact_dirty: Dirty means "might need writing to disk or swap." Takes more work to free. Examples might be files that have not been written to yet. They aren't written to memory too soon in order to keep the I/O down. For instance, if you're writing logs, it might be better to wait until you have a complete log ready before sending it to disk.
  • Inact_clean: Assumed to be easily freeable. The kernel will try to keep some clean stuff around always to have a bit of breathing room.
  • Inact_target: Just a goal metric the kernel uses for making sure there are enough inactive pages around. When exceeded, the kernel will not do work to move pages from active to inactive. A page can also get inactive in a few other ways, e.g. if you do a long sequential I/O, the kernel assumes you're not going to use that memory and makes it inactive preventively. So you can get more inactive pages than the target because the kernel marks some cache as "more likely to be never used" and lets it cheat in the "last used" order.

Memory Statistics

  • HighTotal: is the total amount of memory in the high region. Highmem is all memory above (approx) 860MB of physical RAM. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
  • LowTotal: The total amount of non-highmem memory.
  • LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory.
  • SwapTotal: Total amount of physical swap memory.
  • SwapFree: Total amount of swap memory free.
  • Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. That means, say you do a 1GB malloc, nothing happens, really. Only when you start USING that malloc memory you will get real memory on demand, and just as much as you use. So you sort of take a mortgage and hope the bank doesn't go bust. Other cases might include when you mmap a file that's shared only when you write to it and you get a private copy of that data. While it normally is shared between processes. The Committed_AS is a guesstimate of how much RAM/swap you would need worst-case.