File: fromtop.c

package info (click to toggle)
gtop 0.28.1-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,324 kB
  • ctags: 890
  • sloc: ansic: 6,141; sh: 4,766; cpp: 355; makefile: 281; sed: 93
file content (114 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <sys/time.h>

#include "fromtop.h"

void
sprint_time (char *s, time_t ti, unsigned long hz)
{
	time_t t = ti / hz;
	time_t centi_sec = (ti % 100) * 100 / 100;

#if 0
	fprintf (stderr, "TIME: %ld - %ld - %ld - %ld\n",
		 (long) hz, (long) ti, (long) t, (long) centi_sec);
#endif
	
	if (t < 0)                            /* time overflow */
		sprintf(s, ">>d");
	else if (t >= 48*60*60)               /* > 2 days */
		sprintf(s, "%5lud", t/(24*60*60));
	else if (t >= 60*60)                  /* > 1 hour */
		sprintf(s, "%2lu:%02uh", t/(60*60), (unsigned) ((t/60)%60));
	else if (t > 60)                      /* > 1 minute */
		sprintf(s, "%2lu:%02um", t/60, (unsigned) t%60);
	else
		sprintf(s, "%2lu.%02lus", t, centi_sec);
}

#if 0
void get_etime (char *s, int t, int uptime) {
	/* int t;

	if (stime > uptime*HZ) {
	t = 0;
	} else {
	t = stime);
	}
	sprint_time_ival (s, t/HZ, 6, (t%HZ) * 100 / HZ); */
}

int shift_main (int main) {
	return  main >> PAGE_SHIFT;
}

int pcpu_sort (proc_t ** P, proc_t ** Q)
{
    if ((*P)->pcpu < (*Q)->pcpu)
        return -1;
    if ((*P)->pcpu > (*Q)->pcpu)
        return 1;
    return 0;
}

int mem_sort (proc_t ** P, proc_t ** Q)
{
    if ((*P)->resident < (*Q)->resident)
        return -1;
    if ((*P)->resident > (*Q)->resident)
        return 1;
    return 0;
}
#endif

/*
 * Finds the current time (in microseconds) and calculates the time
 * elapsed since the last update. This is essential for computing
 * percent CPU usage.
 */
float get_elapsed_time(void)
{
    struct timeval time;
    struct timezone timez;
    static double oldtime = 0.0;
    float elapsed_time;
    double newtime;

    gettimeofday (&time, &timez);

    newtime = ((double) time.tv_sec) + ((double) time.tv_usec / 1000000.0);

#if 0
    fprintf (stderr, "ELAPSED: (%ld, %ld) - (%f, %f, %f)\n",
	     (long) time.tv_sec, (long) time.tv_usec,
	     (double) oldtime, (double) newtime,
	     (double) (newtime - oldtime));
#endif

    elapsed_time = (float) (newtime - oldtime);
    oldtime = newtime;

#if 0
    fprintf (stderr, "ELAPSED: %f\n", (double) elapsed_time);
#endif

    return elapsed_time;
}

char * status(proc_t* task) {
    static char buf[4] = "   ";

    buf[0] = task->state;
    if (task->rss == 0 && task->state != 'Z')
        buf[1] = 'W';
    else
        buf[1] = ' ';
    if (task->nice < 0)
	buf[2] = '<';
    else if (task->nice > 0)
	buf[2] = 'N';
    else
	buf[2] = ' ';

    return(buf);
}