wmhdplop: Add version 0.9.9 to repository.
[dockapps.git] / wmhdplop / procstat.c
blobc8994354a3f685c89b56bf0eadf77e8c6fdf55d2
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include "global.h"
5 #include "procstat.h"
6 #include "devnames.h"
8 static ProcStats ps;
9 int use_proc_diskstats;
11 void pstat_init(struct pstat *pst, int nslice, float update_interval) {
12 pst->nslice = nslice;
13 ALLOC_VEC(pst->slices, nslice);
14 pst->cur_slice = 0;
15 pst->total = 0;
16 pst->update_interval = update_interval;
19 float pstat_current(struct pstat *pst) {
20 int idx = pst->cur_slice ? pst->cur_slice-1 : pst->nslice-1;
21 return pst->slices[idx]/pst->update_interval;
24 void pstat_add(struct pstat *pst, unsigned long v) {
25 pst->slices[pst->cur_slice] += v;
28 void pstat_advance(struct pstat *pst) {
29 unsigned long v = pst->slices[pst->cur_slice];
30 if (pst->total)
31 pst->slices[pst->cur_slice] -= pst->total;
32 else pst->slices[pst->cur_slice] = 0;
33 pst->total = v;
34 if (++pst->cur_slice >= pst->nslice) pst->cur_slice = 0;
35 pst->slices[pst->cur_slice] = 0;
38 float pstat_meanval(struct pstat *pst) {
39 unsigned long sum = 0;
40 int i;
41 for (i = 0; i < pst->nslice; ++i) sum += pst->slices[i];
42 return sum / pst->update_interval / (pst->nslice-1);
45 float get_read_throughput() {
46 return pstat_current(&ps.disk_read) / 2048.; /* 2048 because we log sector counts */
49 float get_write_throughput() {
50 return pstat_current(&ps.disk_write) / 2048.;
53 float get_swapin_throughput() {
54 return pstat_current(&ps.swap_in) / 2048.;
57 float get_swapout_throughput() {
58 return pstat_current(&ps.swap_out) / 2048.;
61 float get_read_mean_throughput() {
62 return pstat_meanval(&ps.disk_read) / 2048.;
65 float get_write_mean_throughput() {
66 return pstat_meanval(&ps.disk_write) / 2048.;
69 void update_stats() {
70 FILE *f;
71 char line[1024];
72 char hdname[200];
73 int readok = 0, in_our_disk = 0;
74 const char *proc_fname;
75 if (!use_proc_diskstats) proc_fname = "/proc/partitions";
76 else proc_fname = "/proc/diskstats";
78 f = fopen(proc_fname, "r");
79 if (!f) { perror(proc_fname); return; }
80 while (fgets(line, sizeof(line), f)) {
81 int major, minor;
82 unsigned long nr, nw;
83 const char *fmt = use_proc_diskstats ?
84 "%d %d %200s %*d %*d %lu %*d %*d %*d %lu" :
85 "%d %d %*u %200s %*d %*d %lu %*d %*d %*d %lu";
86 if (sscanf(line, fmt, &major, &minor, hdname, &nr, &nw) == 5 ||
87 (use_proc_diskstats && is_partition(major,minor) &&
88 /* .. kernel 2.5 uses a different format for partitions and disc */
89 sscanf(line, "%d %d %200s %*d %lu %*d %lu", &major, &minor, hdname, &nr, &nw) == 5)) {
90 DiskList *dl;
91 if (readok == 0) readok = 1;
92 if ((dl=find_dev(major,minor))) {
93 dl->touched_r = (dl->nr - nr) ? 10 : dl->touched_r;
94 dl->touched_w = (dl->nw - nw) ? 10 : dl->touched_w;
95 dl->nr = nr; dl->nw = nw;
96 if (!is_partition(major,minor)) in_our_disk = 1;
97 if (is_displayed(dl->hd_id,dl->part_id) &&
98 ((dl->part_id && (!find_id(dl->hd_id, 0) || !is_displayed(dl->hd_id, 0))) || /* partition without host disk */
99 (dl->part_id == 0))) /* disk */
101 if (!Prefs.debug_disk_rd) {
102 pstat_add(&ps.disk_read, nr);
103 } else {
104 static int cntr = 0; cntr+=(rand()%30) == 0 ? Prefs.debug_disk_rd : 0;
105 pstat_add(&ps.disk_read, nr + cntr);
107 if (!Prefs.debug_disk_wr) {
108 pstat_add(&ps.disk_write, nw);
109 } else {
110 static int cntw = 0; cntw+=(rand()%30) == 0 ? Prefs.debug_disk_wr : 0;
111 pstat_add(&ps.disk_write, nw + cntw);
113 readok = 2;
115 } else if (!is_partition(major,minor)) in_our_disk = 0;
116 /* if (in_our_disk || find_dev(major,minor))*/ {
117 strlist *sl;
118 for (sl = swap_list(); sl; sl=sl->next) {
119 if (strcmp(stripdev(hdname), stripdev(sl->s)) == 0) {
120 if (!Prefs.debug_swapio) {
121 pstat_add(&ps.swap_in, nr);
122 pstat_add(&ps.swap_out, nw);
123 } else {
124 static int cnt = 0; cnt+=Prefs.debug_swapio;
125 pstat_add(&ps.swap_in, nr + cnt);
126 pstat_add(&ps.swap_out, nw + cnt);
133 fclose(f);
134 pstat_advance(&ps.disk_read);pstat_advance(&ps.disk_write);
135 pstat_advance(&ps.swap_in);pstat_advance(&ps.swap_out);
136 if (readok == 0) { fprintf(stderr, "warning: could not find any info in %s (kernel too old?)\n", proc_fname); exit(1); }
137 else if (readok == 1) { ONLY_ONCE(fprintf(stderr, "warning: could not find any monitored disc in %s\n", proc_fname)); }
138 BLAHBLAH(1,printf("swap: %5.2f,%5.2f disc: %5.2f,%5.2f MB/s\n",
139 get_swapin_throughput(), get_swapout_throughput(),
140 get_read_throughput(), get_write_throughput()));
143 void init_stats(float update_interval) {
144 char s[512];
145 FILE *f;
147 pstat_init(&ps.swap_in, (int)(0.5/update_interval)+1, update_interval);
148 pstat_init(&ps.swap_out, (int)(0.5/update_interval)+1, update_interval);
149 pstat_init(&ps.disk_read, (int)(0.5/update_interval)+1, update_interval);
150 pstat_init(&ps.disk_write, (int)(0.5/update_interval)+1, update_interval);
151 f = fopen("/proc/swaps","r");
152 //if (!f) { perror("/proc/swaps"); exit(1); }
153 if (f) {
154 while (fgets(s, 512, f)) {
155 char *s2 = strchr(s,' ');
156 if (s2 && s2 != s && strncmp(s, "/dev/", 5) == 0) {
157 *s2 = 0;
158 add_swap(s);
159 BLAHBLAH(1,printf("found swap partition: %s\n", swap_list()->s));
162 fclose(f);
164 if (!swap_list()) {
165 fprintf(stderr, "Warning: no swap partition found in /proc/swaps !!\n");
167 if ((f=fopen("/proc/diskstats","r"))) {
168 use_proc_diskstats = 1; fclose(f);
170 else { use_proc_diskstats = 0; }
171 BLAHBLAH(1,printf("using %s for disc statistics\n", use_proc_diskstats ? "/proc/diskstats" : "/proc/partitions"));
176 /* hello I am brain-dead */
177 void scan_all_hd(int add_partitions) {
178 char s[512];
179 FILE *f = NULL;
180 /* find mounted partitions */
181 if (add_partitions && (f = fopen("/etc/mtab","r"))) {
182 while (fgets(s,512,f)) {
183 char partname[512], mountpoint[512];
184 mountpoint[0] = 0;
185 if (sscanf(s,"%500s %500s", partname, mountpoint)>=1) {
186 char *p = strchr(mountpoint, '/');
187 //printf("add_device_by_name(%s, %s)\n", partname, p);
188 add_device_by_name(partname, p);
192 if (f) fclose(f);
194 /* second try .. look for hard drives listed in proc/partitions (and only hard-drives .. their partitions
195 are supposed to be listed in mtab) */
196 if ((f = fopen("/proc/partitions","r"))) {
197 while (fgets(s,512,f)) {
198 unsigned major, minor;
199 char name[512]; name[0] = 0;
200 if (sscanf(s,"%d %d %*d %500s", &major, &minor, name)==3) {
201 int hd_id, part_id;
202 /* on n'ajoute que les disques dont au moins une partoche etait dans mtab */
203 if (device_info(major,minor,NULL,&hd_id,&part_id) != 0 && part_id == 0 && find_id(hd_id,-1)) {
204 add_device_by_id(major,minor,NULL);
210 if (f) fclose(f);
211 /* for (c = 'a'; c <= 'z'; ++c) {
212 snprintf(s,512,"/proc/ide/hd%c/media",c);
213 if ((f = fopen(s,"r")) && fgets(s, 512, f) && strcmp(s, "disk\n")==0) {
214 snprintf(s, 512, "/dev/hd%c",c);
215 add_device(s, NULL);
217 if (f) fclose(f);