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