summaryrefslogtreecommitdiff
path: root/src/pmdas/linux_proc/proc_runq.c
blob: 07b68dc30b471e6d74789952a3157a19321df2af (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
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
115
116
117
118
119
120
121
122
123
/*
 * Linux /proc/runq metrics cluster
 *
 * Copyright (c) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>
#include "proc_pid.h"
#include "proc_runq.h"

int
refresh_proc_runq(proc_runq_t *proc_runq)
{
    int sz;
    int fd;
    char *p;
    int sname;
    DIR *dir;
    struct dirent *d;
    char fullpath[MAXPATHLEN];
    char buf[4096];

    memset(proc_runq, 0, sizeof(proc_runq_t));
    if ((dir = opendir("/proc")) == NULL)
    	return -oserror();

    while((d = readdir(dir)) != NULL) {
	if (!isdigit((int)d->d_name[0]))
	    continue;
	sprintf(fullpath, "/proc/%s/stat", d->d_name);
	if ((fd = open(fullpath, O_RDONLY)) < 0)
	    continue;
	sz = read(fd, buf, sizeof(buf));
	close(fd);
	buf[sizeof(buf)-1] = '\0';

	/*
	 * defunct (state name is 'Z')
	 */
	if (sz <= 0 || (p = _pm_getfield(buf, PROC_PID_STAT_STATE)) == NULL) {
	    proc_runq->unknown++;
	    continue;
	}
	if ((sname = *p) == 'Z') {
	    proc_runq->defunct++;
	    continue;
	}

	/*
	 * kernel process (not defunct and virtual size is zero)
	 */
	if ((p = _pm_getfield(buf, PROC_PID_STAT_VSIZE)) == NULL) {
	    proc_runq->unknown++;
	    continue;
	}
	if (strcmp(p, "0") == 0) {
	    proc_runq->kernel++;
	    continue;
	}

	/*
	 * swapped (resident set size is zero)
	 */
	if ((p = _pm_getfield(buf, PROC_PID_STAT_RSS)) == NULL) {
	    proc_runq->unknown++;
	    continue;
	}
	if (strcmp(p, "0") == 0) {
	    proc_runq->swapped++;
	    continue;
	}

	/*
	 * All other states
	 */
	switch (sname) {
	case 'R':
	    proc_runq->runnable++;
	    break;
	case 'S':
	    proc_runq->sleeping++;
	    break;
	case 'T':
	    proc_runq->stopped++;
	    break;
	case 'D':
	    proc_runq->blocked++;
	    break;
	/* case 'Z':
	    break; -- already counted above */
	default:
	    fprintf(stderr, "UNKNOWN %c : %s\n", sname, buf);
	    proc_runq->unknown++;
	    break;
	}
    }
    closedir(dir);

#if PCP_DEBUG
    if (pmDebug & DBG_TRACE_LIBPMDA) {
	fprintf(stderr, "refresh_runq: runnable=%d sleeping=%d stopped=%d blocked=%d unknown=%d\n",
	    proc_runq->runnable, proc_runq->sleeping, proc_runq->stopped,
	    proc_runq->blocked, proc_runq->unknown);
    }
#endif

    return 0;
}