summaryrefslogtreecommitdiff
path: root/core/procps/procps-3.2.7-top-env-cpuloop.patch
blob: 0034dcafb9edaa7dfddcdf88a255f7a8b0a2946c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
diff -up procps-3.2.7/top.c.p15 procps-3.2.7/top.c
--- procps-3.2.7/top.c.p15	2008-09-01 11:37:51.000000000 +0200
+++ procps-3.2.7/top.c	2008-09-01 11:37:51.000000000 +0200
@@ -121,6 +121,7 @@ static int No_ksyms = -1,       // set t
            Batch = 0,           // batch mode, collect no input, dumb output
            Loops = -1,          // number of iterations, -1 loops forever
            Secure_mode = 0;     // set if some functionality restricted
+static int CPU_loop = 0;	// wait for reliable CPU data
 
         /* Some cap's stuff to reduce runtime calls --
            to accomodate 'Batch' mode, they begin life as empty strings */
@@ -1679,6 +1680,9 @@ static void before (char *me)
       Fieldstab[P_PPD].fmts = pid_fmt;
       Fieldstab[P_PPD].head = "       PPID" + 10 - pid_digits;
    }
+
+   if (( getenv("CPULOOP")) && ( atoi(getenv("CPULOOP")) == 1 )) 
+	   CPU_loop = 1;
 }
 
 
@@ -2978,6 +2981,7 @@ static proc_t **summary_show (void)
 {
    static proc_t **p_table = NULL;
    static CPU_t  *smpcpu = NULL;
+   static int first = 0;
 
    // whoa first time, gotta' prime the pump...
    if (!p_table) {
@@ -3025,6 +3029,28 @@ static proc_t **summary_show (void)
 
       smpcpu = cpus_refresh(smpcpu);
 
+      if (first==0 && CPU_loop)
+      {
+	 int i;
+	 CPU_t *cpu;
+	 for (i = 0; i < Cpu_tot; i++) {
+           cpu =  &smpcpu[i];
+	   cpu->u_sav = cpu->u;
+	   cpu->s_sav = cpu->s;
+	   cpu->n_sav = cpu->n;
+	   cpu->i_sav = cpu->i;
+	   cpu->w_sav = cpu->w;
+	   cpu->x_sav = cpu->x;
+	   cpu->y_sav = cpu->y;
+	   cpu->z_sav = cpu->z;
+	 }
+         tv.tv_sec = Rc.delay_time;
+         tv.tv_usec = (Rc.delay_time - (int)Rc.delay_time) * 1000000;
+         select(0, NULL, NULL, NULL, &tv);
+
+	 smpcpu = cpus_refresh(smpcpu);
+      }
+
       if (CHKw(Curwin, View_CPUSUM)) {
          // display just the 1st /proc/stat line
          summaryhlp(&smpcpu[Cpu_tot], "Cpu(s):");
@@ -3050,6 +3076,8 @@ static proc_t **summary_show (void)
    }
 
    SETw(Curwin, NEWFRAM_cwo);
+
+   first = 1;
    return p_table;
 }
 
diff -up procps-3.2.7/top.1.p15 procps-3.2.7/top.1
--- procps-3.2.7/top.1.p15	2008-09-01 11:37:51.000000000 +0200
+++ procps-3.2.7/top.1	2008-09-01 11:37:51.000000000 +0200
@@ -1183,6 +1183,13 @@ Then ponder this:
 Send bug reports to:
    Albert D\. Cahalan, <albert@users.sf.net>
 
+The top command calculates Cpu(s) by looking at the change in CPU time values
+between samples. When you first run it, it has no previous sample to compare
+to, so these initial values are the percentages since boot. It means you need
+at least two loops or you have to ignore summary output from the first loop.
+This is problem for example for batch mode. There is a possible workaround if
+you define the CPULOOP=1 environment variable. The top command will be run one
+extra hidden loop for CPU data before standard output.
 
 .\" ----------------------------------------------------------------------
 .SH 8. HISTORY Former top