~ahasenack/landscape-client/landscape-client-1.5.5-0ubuntu0.9.04.0

« back to all changes in this revision

Viewing changes to landscape/lib/jiffies.py

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-09-08 16:35:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908163557-l3ixzj5dxz37wnw2
Tags: 1.0.18-0ubuntu1
New upstream release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
 
 
3
 
 
4
def detect_jiffies():
 
5
    """Returns the number of jiffies per second for this machine.
 
6
 
 
7
    A jiffy is a value used by the kernel to report certain time-based
 
8
    events.  Jiffies occur N times per second where N varies depending
 
9
    on the hardware the kernel is running on.  This function gets the
 
10
    uptime for the current process, forks a child process and gets the
 
11
    uptime again; finally, using the running time of the child process
 
12
    compared with the uptimes to determine number of jiffies per
 
13
    second.
 
14
    """
 
15
    uptime1_file = open("/proc/uptime")
 
16
    uptime2_file = open("/proc/uptime")
 
17
    read_uptime1 = uptime1_file.read
 
18
    read_uptime2 = uptime2_file.read
 
19
 
 
20
    while True:
 
21
        uptime1_data = read_uptime1()
 
22
 
 
23
        # Fork a process and exit immediately; this results in the
 
24
        # child process being left around as a zombie until waitpid()
 
25
        # is called.
 
26
        pid = os.fork()
 
27
        if pid == 0:
 
28
            os._exit(0)
 
29
 
 
30
        uptime2_data = read_uptime2()
 
31
 
 
32
        stat_file = open("/proc/%d/stat" % pid)
 
33
        stat_data = stat_file.read()
 
34
        stat_file.close()
 
35
 
 
36
        os.waitpid(pid, 0)
 
37
 
 
38
        seconds_uptime1 = float(uptime1_data.split()[0])
 
39
        seconds_uptime2 = float(uptime2_data.split()[0])
 
40
        jiffie_uptime = int(stat_data.split()[21])
 
41
 
 
42
        jiffies1 = int(jiffie_uptime/seconds_uptime1+0.5)
 
43
        jiffies2 = int(jiffie_uptime/seconds_uptime2+0.5)
 
44
 
 
45
        if jiffies1 == jiffies2:
 
46
            break
 
47
 
 
48
        uptime1_file.seek(0)
 
49
        uptime2_file.seek(0)
 
50
 
 
51
    uptime1_file.close()
 
52
    uptime2_file.close()
 
53
    return jiffies1