Linux – How to detect exceptionally long file locks in linux

linuxlockingprocess

I have a point of contention on my linux server. One of a number of processes access a single file and lock the file at a random time for a considerable period (>60 seconds) which, in turn, causes other things to fail.

Is there a way to detect how long a file has been locked and by which process?

Best Answer

I think what you are looking for is the file /proc/locks. It shows the current file locks in the system. This not shows how long a file has been locked, but it shows by which process. Maybe you could detect when the lock is register in this file and measure the elapsed time. A sample is this:

cat /proc/locks 
1: POSIX  ADVISORY  WRITE 2245 08:06:1182714 1073741824 1073741824
2: POSIX  ADVISORY  WRITE 2245 08:06:1182714 1073741826 1073742335
3: POSIX  ADVISORY  WRITE 3058 08:06:10752740 0 0
4: POSIX  ADVISORY  WRITE 3058 08:06:10752739 0 0
5: POSIX  ADVISORY  WRITE 2421 08:06:10752766 0 EOF
6: POSIX  ADVISORY  WRITE 2421 08:06:11142048 0 EOF
7: POSIX  ADVISORY  WRITE 2421 08:06:9964366 1073741824 1073742335
8: POSIX  ADVISORY  WRITE 2421 08:06:11142040 0 EOF

Where the columns are:

  • First: lock #.
  • Second: lock type (POSIX if the lock was created with fcntl and FLOCK if created with flock.
  • Third: lock mode (ADVISORY or MANDATORY)
  • Forth: Lock type (WRITE or READ), corresponding to locks shared or exclusive.
  • Fifth: PID of the process with the lock.
  • Sixth: Three numbers separated by : that identified the locked file.
  • Seventh: Start byte of the lock.
  • Eighth: End byte of the lock