forked from ProcursusTeam/launchctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runstats.c
136 lines (126 loc) · 4.92 KB
/
runstats.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2023 Procursus Team <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/resource.h>
#include <xpc/xpc.h>
#include "xpc_private.h"
#include "launchctl.h"
static void
print_runstats(size_t index, xpc_object_t dict, void *ctx)
{
size_t ru_len = 0;
const struct rusage* ru = (const struct rusage*)xpc_dictionary_get_data(dict, "rusage", &ru_len);
if (ru_len != sizeof(struct rusage)) {
fprintf(stderr, "runstats ipc routine returned incorrectly sized struct rusage\n");
exit(1);
}
int64_t pid = xpc_dictionary_get_int64(dict, "pid");
int64_t reason = xpc_dictionary_get_int64(dict, "run-reason");
uint64_t start = xpc_dictionary_get_uint64(dict, "start");
uint64_t end = xpc_dictionary_get_uint64(dict, "end");
uint64_t forks = xpc_dictionary_get_uint64(dict, "forks");
uint64_t execs = xpc_dictionary_get_uint64(dict, "execs");
bool dirty_exit = xpc_dictionary_get_bool(dict, "dirty-exit");
bool idle_exit = xpc_dictionary_get_bool(dict, "idle-exit");
bool jettisoned = xpc_dictionary_get_bool(dict, "jettisoned");
printf("run %lu = {\n", index);
printf("\tpid = %lld\n", pid);
printf("\treason = %lld\n", reason);
printf("\tstart = %llu\n", start);
printf("\tend = %llu\n", end);
printf("\tforks = %llu\n", forks);
printf("\texecs = %llu\n", execs);
printf("\tdirty exit = %d\n", dirty_exit);
printf("\tidle exit = %d\n", idle_exit);
printf("\tjettisoned = %d\n", jettisoned);
printf("\trusage = {\n");
printf("\t\tuser time = %ld\n", ru->ru_utime.tv_sec);
printf("\t\tsystem time = %ld\n", ru->ru_stime.tv_sec);
printf("\t\tmax resident set = %ld\n", ru->ru_maxrss);
printf("\t\tintegral shared memory size = %ld\n", ru->ru_ixrss);
printf("\t\tintegral unshared data size = %ld\n", ru->ru_idrss);
printf("\t\tintegral unshared stack size = %ld\n", ru->ru_isrss);
printf("\t\tpage reclaims = %ld\n", ru->ru_minflt);
printf("\t\tpage faults = %ld\n", ru->ru_majflt);
printf("\t\tswaps = %ld\n", ru->ru_nswap);
printf("\t\tblock input operations = %ld\n", ru->ru_inblock);
printf("\t\tblock input operations = %ld\n", ru->ru_inblock);
printf("\t\tblock output operations = %ld\n", ru->ru_oublock);
printf("\t\tmessages sent = %ld\n", ru->ru_msgsnd);
printf("\t\tmessages received = %ld\n", ru->ru_msgrcv);
printf("\t\tsignals received = %ld\n", ru->ru_nsignals);
printf("\t\tvoluntary context switches = %ld\n", ru->ru_nvcsw);
printf("\t\tinvoluntary context switches = %ld\n", ru->ru_nivcsw);
printf("\t}\n");
printf("}\n");
return;
}
int
runstats_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple)
{
if (argc != 2)
return EUSAGE;
xpc_object_t dict, reply;
const char *name = NULL;
xpc_object_t runs;
int ret;
dict = xpc_dictionary_create(NULL, NULL, 0);
*msg = dict;
if ((ret = launchctl_setup_xpc_dict_for_service_name(argv[1], dict, &name))) {
return ret;
}
if (name == NULL)
return EBADNAME;
ret = launchctl_send_xpc_to_launchd(XPC_ROUTINE_RUNSTATS, dict, &reply);
if (ret == ENOTSUP) {
fprintf(stderr, "Performance logging is not enabled.\n");
return ret;
} else if (ret == ENOENT) {
fprintf(stderr, "No resource statistics gathered for service yet.\n");
return ret;
} else if (ret == EINVAL) {
fprintf(stderr, "Bad Request.\n");
return ret;
} else if (ret) {
return ret;
}
ret = 0;
runs = xpc_dictionary_get_value(reply, "runs");
if (runs == NULL || xpc_get_type(runs) != XPC_TYPE_ARRAY) {
return EINVAL;
}
printf("\"%s\"\n", name);
xpc_array_apply_f(runs, NULL, print_runstats);
return ret;
}