xref: /6.0.3/sigar/programs/sigar_port.c (revision abc7020b)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2011 Couchbase, Inc.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17#include <stdlib.h>
18#include <stdio.h>
19#include <stdint.h>
20#include <string.h>
21#include <sigar.h>
22
23#ifdef _WIN32
24#include <io.h>
25#include <fcntl.h>
26#endif
27
28#define DEFAULT(value, def) ((value) == SIGAR_FIELD_NOTIMPL ? (def) : (value))
29#define MUST_SUCCEED(body)                      \
30    do {                                        \
31        int _ret = (body);                      \
32        if (_ret != SIGAR_OK) {                 \
33            exit(1);                            \
34        }                                       \
35    } while (0)
36
37#define NUM_INTERESTING_PROCS 40
38#define PROCS_REFRESH_INTERVAL 20
39#define PROC_NAME_LEN 12
40
41struct proc {
42    sigar_pid_t pid;
43    sigar_uint64_t start_time;
44    char name[PROC_NAME_LEN];
45};
46
47struct proc_stats {
48    char name[PROC_NAME_LEN];
49    uint32_t cpu_utilization;
50
51    uint64_t pid;
52
53    uint64_t mem_size;
54    uint64_t mem_resident;
55    uint64_t mem_share;
56    uint64_t minor_faults;
57    uint64_t major_faults;
58    uint64_t page_faults;
59};
60
61struct system_stats {
62    uint32_t version;
63    uint32_t struct_size;
64
65    uint64_t cpu_total_ms;
66    uint64_t cpu_idle_ms;
67
68    uint64_t swap_total;
69    uint64_t swap_used;
70    uint64_t swap_page_in;
71    uint64_t swap_page_out;
72
73    uint64_t mem_total;
74    uint64_t mem_used;
75    uint64_t mem_actual_used;
76    uint64_t mem_actual_free;
77
78    struct proc_stats interesting_procs[NUM_INTERESTING_PROCS];
79};
80
81static int find_interesting_procs(sigar_t *sigar, sigar_pid_t parent,
82                                  struct proc procs[NUM_INTERESTING_PROCS])
83{
84    unsigned long i;
85    int found = 0;
86
87    sigar_proc_list_t proc_list;
88    sigar_proc_state_t proc_state;
89    sigar_proc_cpu_t proc_cpu;
90    sigar_pid_t pid;
91
92    MUST_SUCCEED(sigar_proc_list_get(sigar, &proc_list));
93
94    for (i = 0; i < proc_list.number; ++i) {
95        pid = proc_list.data[i];
96
97        if (sigar_proc_state_get(sigar, pid, &proc_state) != SIGAR_OK) {
98            continue;
99        }
100
101        if (proc_state.ppid == parent &&
102            strcmp(proc_state.name, "moxi") != 0) {
103
104            procs[found].pid = pid;
105            strncpy(procs[found].name, proc_state.name, PROC_NAME_LEN);
106
107            if (sigar_proc_cpu_get(sigar, pid, &proc_cpu) != SIGAR_OK) {
108                continue;
109            }
110
111            procs[found].start_time = proc_cpu.start_time;
112            ++found;
113
114            if (found == NUM_INTERESTING_PROCS) {
115                break;
116            }
117        }
118    }
119
120    MUST_SUCCEED(sigar_proc_list_destroy(sigar, &proc_list));
121
122    return found;
123}
124
125static int populate_interesting_procs(sigar_t *sigar,
126                                      struct proc *procs, int procs_count,
127                                      struct system_stats *reply)
128{
129    int i;
130    int stale = 0;
131
132    sigar_proc_mem_t proc_mem;
133    sigar_proc_cpu_t proc_cpu;
134
135    struct proc_stats *child;
136
137    for (i = 0; i < procs_count; ++i) {
138        if (sigar_proc_mem_get(sigar, procs[i].pid, &proc_mem) != SIGAR_OK ||
139            sigar_proc_cpu_get(sigar, procs[i].pid, &proc_cpu) != SIGAR_OK ||
140            procs[i].start_time != proc_cpu.start_time)
141        {
142            stale = 1;
143            continue;
144        }
145
146        child = &reply->interesting_procs[i];
147
148        child->pid = procs[i].pid;
149        strncpy(child->name, procs[i].name, PROC_NAME_LEN);
150        child->cpu_utilization = DEFAULT((uint32_t) (100 * proc_cpu.percent), 0);
151        child->mem_size = DEFAULT(proc_mem.size, 0);
152        child->mem_resident = DEFAULT(proc_mem.resident, 0);
153        child->mem_share = DEFAULT(proc_mem.share, 0);
154        child->minor_faults = DEFAULT(proc_mem.minor_faults, 0);
155        child->major_faults = DEFAULT(proc_mem.major_faults, 0);
156        child->page_faults = DEFAULT(proc_mem.page_faults, 0);
157    }
158
159    return stale;
160}
161
162int main(void)
163{
164    sigar_t *sigar;
165    sigar_mem_t mem;
166    sigar_swap_t swap;
167    sigar_cpu_t cpu;
168    struct system_stats reply;
169
170    sigar_pid_t pid;
171    sigar_proc_state_t state;
172
173    sigar_pid_t child_vm_pid;
174    sigar_pid_t babysitter_pid;
175
176    int procs_stale = 1;
177    int procs_count;
178    struct proc procs[NUM_INTERESTING_PROCS];
179
180    int ticks_to_refresh = PROCS_REFRESH_INTERVAL;
181
182    MUST_SUCCEED(sigar_open(&sigar));
183
184    pid = sigar_pid_get(sigar);
185    MUST_SUCCEED(sigar_proc_state_get(sigar, pid, &state));
186    child_vm_pid = state.ppid;
187
188    MUST_SUCCEED(sigar_proc_state_get(sigar, child_vm_pid, &state));
189    babysitter_pid = state.ppid;
190
191#ifdef _WIN32
192    _setmode(1, _O_BINARY);
193    _setmode(0, _O_BINARY);
194#endif
195
196    while (!feof(stdin)) {
197        int req;
198        int rv = fread(&req, sizeof(req), 1, stdin);
199        if (rv < 1) {
200            continue;
201        }
202        if (req != 0) {
203            break;
204        }
205        memset(&reply, 0, sizeof(reply));
206        reply.version = 2;
207        reply.struct_size = sizeof(reply);
208
209        sigar_mem_get(sigar, &mem);
210        sigar_swap_get(sigar, &swap);
211        sigar_cpu_get(sigar, &cpu);
212
213        reply.cpu_total_ms = cpu.total;
214        reply.cpu_idle_ms = cpu.idle + cpu.wait;
215
216        reply.swap_total = swap.total;
217        reply.swap_used = swap.used;
218        reply.swap_page_in = swap.page_in;
219        reply.swap_page_out = swap.page_out;
220
221        reply.mem_total = mem.total;
222        reply.mem_used = mem.used;
223        reply.mem_actual_used = mem.actual_used;
224        reply.mem_actual_free = mem.actual_free;
225
226        if (procs_stale || ticks_to_refresh-- == 0) {
227            ticks_to_refresh = PROCS_REFRESH_INTERVAL;
228            procs_count = find_interesting_procs(sigar, babysitter_pid, procs);
229        }
230
231        procs_stale = populate_interesting_procs(sigar, procs, procs_count, &reply);
232
233        fwrite(&reply, sizeof(reply), 1, stdout);
234        fflush(stdout);
235    }
236
237    return 0;
238}
239