1 /* Copyright (C) 2015-2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
5 #include "daemon/bindings/impl.h"
6
7
getseconds(uv_timeval_t * tv)8 static inline double getseconds(uv_timeval_t *tv)
9 {
10 return (double)tv->tv_sec + 0.000001*((double)tv->tv_usec);
11 }
12
13 /** Return worker statistics. */
wrk_stats(lua_State * L)14 static int wrk_stats(lua_State *L)
15 {
16 struct worker_ctx *worker = the_worker;
17 if (!worker) {
18 return 0;
19 }
20 lua_newtable(L);
21 lua_pushnumber(L, worker->stats.queries);
22 lua_setfield(L, -2, "queries");
23 lua_pushnumber(L, worker->stats.concurrent);
24 lua_setfield(L, -2, "concurrent");
25 lua_pushnumber(L, worker->stats.dropped);
26 lua_setfield(L, -2, "dropped");
27
28 lua_pushnumber(L, worker->stats.timeout);
29 lua_setfield(L, -2, "timeout");
30 lua_pushnumber(L, worker->stats.udp);
31 lua_setfield(L, -2, "udp");
32 lua_pushnumber(L, worker->stats.tcp);
33 lua_setfield(L, -2, "tcp");
34 lua_pushnumber(L, worker->stats.tls);
35 lua_setfield(L, -2, "tls");
36 lua_pushnumber(L, worker->stats.ipv4);
37 lua_setfield(L, -2, "ipv4");
38 lua_pushnumber(L, worker->stats.ipv6);
39 lua_setfield(L, -2, "ipv6");
40 lua_pushnumber(L, worker->stats.err_udp);
41 lua_setfield(L, -2, "err_udp");
42 lua_pushnumber(L, worker->stats.err_tcp);
43 lua_setfield(L, -2, "err_tcp");
44 lua_pushnumber(L, worker->stats.err_tls);
45 lua_setfield(L, -2, "err_tls");
46 lua_pushnumber(L, worker->stats.err_http);
47 lua_setfield(L, -2, "err_http");
48
49 /* Add subset of rusage that represents counters. */
50 uv_rusage_t rusage;
51 if (uv_getrusage(&rusage) == 0) {
52 lua_pushnumber(L, getseconds(&rusage.ru_utime));
53 lua_setfield(L, -2, "usertime");
54 lua_pushnumber(L, getseconds(&rusage.ru_stime));
55 lua_setfield(L, -2, "systime");
56 lua_pushnumber(L, rusage.ru_majflt);
57 lua_setfield(L, -2, "pagefaults");
58 lua_pushnumber(L, rusage.ru_nswap);
59 lua_setfield(L, -2, "swaps");
60 lua_pushnumber(L, rusage.ru_nvcsw + rusage.ru_nivcsw);
61 lua_setfield(L, -2, "csw");
62 }
63 /* Get RSS */
64 size_t rss = 0;
65 if (uv_resident_set_memory(&rss) == 0) {
66 lua_pushnumber(L, rss);
67 lua_setfield(L, -2, "rss");
68 }
69 return 1;
70 }
71
kr_bindings_worker(lua_State * L)72 int kr_bindings_worker(lua_State *L)
73 {
74 static const luaL_Reg lib[] = {
75 { "stats", wrk_stats },
76 { NULL, NULL }
77 };
78 luaL_register(L, "worker", lib);
79 return 1;
80 }
81
82