xref: /6.0.3/sigar/programs/sigar_port.c (revision 911cc028)
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 <stdio.h>
18#include <stdint.h>
19#include <string.h>
20#include <sigar.h>
21
22#ifdef _WIN32
23#include <io.h>
24#include <fcntl.h>
25#endif
26
27#define DEFAULT(value, def) ((value) == SIGAR_FIELD_NOTIMPL ? (def) : (value))
28
29#define NUM_INTERESTING_PROCS 10
30#define PROC_NAME_LEN 12
31
32struct proc {
33    sigar_pid_t pid;
34    sigar_uint64_t start_time;
35    char name[PROC_NAME_LEN];
36};
37
38struct proc_stats {
39    char name[PROC_NAME_LEN];
40    uint32_t cpu_utilization;
41
42    uint64_t pid;
43
44    uint64_t mem_size;
45    uint64_t mem_resident;
46    uint64_t mem_share;
47    uint64_t minor_faults;
48    uint64_t major_faults;
49    uint64_t page_faults;
50};
51
52struct system_stats {
53    uint32_t version;
54    uint32_t struct_size;
55
56    uint64_t cpu_total_ms;
57    uint64_t cpu_idle_ms;
58
59    uint64_t swap_total;
60    uint64_t swap_used;
61    uint64_t swap_page_in;
62    uint64_t swap_page_out;
63
64    uint64_t mem_total;
65    uint64_t mem_used;
66    uint64_t mem_actual_used;
67    uint64_t mem_actual_free;
68
69    struct proc_stats interesting_procs[NUM_INTERESTING_PROCS];
70};
71
72static int find_interesting_procs(sigar_t *sigar, sigar_pid_t parent,
73                                  struct proc procs[NUM_INTERESTING_PROCS])
74{
75    unsigned long i;
76    int found = 0;
77
78    sigar_proc_list_t proc_list;
79    sigar_proc_state_t proc_state;
80    sigar_proc_cpu_t proc_cpu;
81    sigar_pid_t pid;
82
83    sigar_proc_list_get(sigar, &proc_list);
84
85    for (i = 0; i < proc_list.number; ++i) {
86        pid = proc_list.data[i];
87
88        if (sigar_proc_state_get(sigar, pid, &proc_state) != SIGAR_OK) {
89            continue;
90        }
91
92        if (proc_state.ppid == parent &&
93            strcmp(proc_state.name, "moxi") != 0) {
94
95            procs[found].pid = pid;
96            strncpy(procs[found].name, proc_state.name, PROC_NAME_LEN);
97
98            if (sigar_proc_cpu_get(sigar, pid, &proc_cpu) != SIGAR_OK) {
99                continue;
100            }
101
102            procs[found].start_time = proc_cpu.start_time;
103            ++found;
104
105            if (found == NUM_INTERESTING_PROCS) {
106                break;
107            }
108        }
109    }
110
111    sigar_proc_list_destroy(sigar, &proc_list);
112
113    return found;
114}
115
116static int populate_interesting_procs(sigar_t *sigar,
117                                      struct proc *procs, int procs_count,
118                                      struct system_stats *reply)
119{
120    int i;
121    int stale = 0;
122
123    sigar_proc_mem_t proc_mem;
124    sigar_proc_cpu_t proc_cpu;
125
126    struct proc_stats *child;
127
128    for (i = 0; i < procs_count; ++i) {
129        if (sigar_proc_mem_get(sigar, procs[i].pid, &proc_mem) != SIGAR_OK ||
130            sigar_proc_cpu_get(sigar, procs[i].pid, &proc_cpu) != SIGAR_OK ||
131            procs[i].start_time != proc_cpu.start_time)
132        {
133            stale = 1;
134            continue;
135        }
136
137        child = &reply->interesting_procs[i];
138
139        child->pid = procs[i].pid;
140        strncpy(child->name, procs[i].name, PROC_NAME_LEN);
141        child->cpu_utilization = DEFAULT((uint32_t) (100 * proc_cpu.percent), 0);
142        child->mem_size = DEFAULT(proc_mem.size, 0);
143        child->mem_resident = DEFAULT(proc_mem.resident, 0);
144        child->mem_share = DEFAULT(proc_mem.share, 0);
145        child->minor_faults = DEFAULT(proc_mem.minor_faults, 0);
146        child->major_faults = DEFAULT(proc_mem.major_faults, 0);
147        child->page_faults = DEFAULT(proc_mem.page_faults, 0);
148    }
149
150    return stale;
151}
152
153int main(void)
154{
155    sigar_t *sigar;
156    sigar_mem_t mem;
157    sigar_swap_t swap;
158    sigar_cpu_t cpu;
159    struct system_stats reply;
160
161    sigar_pid_t pid;
162    sigar_proc_state_t state;
163
164    sigar_pid_t child_vm_pid;
165    sigar_pid_t babysitter_pid;
166
167    int procs_stale = 1;
168    int procs_count;
169    struct proc procs[NUM_INTERESTING_PROCS];
170
171    sigar_open(&sigar);
172
173    pid = sigar_pid_get(sigar);
174    sigar_proc_state_get(sigar, pid, &state);
175    child_vm_pid = state.ppid;
176
177    sigar_proc_state_get(sigar, child_vm_pid, &state);
178    babysitter_pid = state.ppid;
179
180#ifdef _WIN32
181    _setmode(1, _O_BINARY);
182    _setmode(0, _O_BINARY);
183#endif
184
185    while (!feof(stdin)) {
186        int req;
187        int rv = fread(&req, sizeof(req), 1, stdin);
188        if (rv < 1) {
189            continue;
190        }
191        if (req != 0) {
192            break;
193        }
194        memset(&reply, 0, sizeof(reply));
195        reply.version = 2;
196        reply.struct_size = sizeof(reply);
197
198        sigar_mem_get(sigar, &mem);
199        sigar_swap_get(sigar, &swap);
200        sigar_cpu_get(sigar, &cpu);
201
202        reply.cpu_total_ms = cpu.total;
203        reply.cpu_idle_ms = cpu.idle + cpu.wait;
204
205        reply.swap_total = swap.total;
206        reply.swap_used = swap.used;
207        reply.swap_page_in = swap.page_in;
208        reply.swap_page_out = swap.page_out;
209
210        reply.mem_total = mem.total;
211        reply.mem_used = mem.used;
212        reply.mem_actual_used = mem.actual_used;
213        reply.mem_actual_free = mem.actual_free;
214
215        if (procs_stale) {
216            procs_count = find_interesting_procs(sigar, babysitter_pid, procs);
217        }
218
219        procs_stale = populate_interesting_procs(sigar, procs, procs_count, &reply);
220
221        if (procs_count != NUM_INTERESTING_PROCS) {
222            procs_stale = 1;
223        }
224
225        fwrite(&reply, sizeof(reply), 1, stdout);
226        fflush(stdout);
227    }
228
229    return 0;
230}
231