1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "private-lib-core.h"
26
27 int
lws_det_lat_active(struct lws_context * context)28 lws_det_lat_active(struct lws_context *context)
29 {
30 return !!context->detailed_latency_cb;
31 }
32
33 int
lws_det_lat_cb(struct lws_context * context,lws_detlat_t * d)34 lws_det_lat_cb(struct lws_context *context, lws_detlat_t *d)
35 {
36 int n;
37
38 if (!context->detailed_latency_cb)
39 return 0;
40
41 n = context->detailed_latency_cb(context, d);
42
43 memset(&d->latencies, 0, sizeof(d->latencies));
44
45 return n;
46 }
47
48 static const char types[] = "rwNCTt????";
49 int
lws_det_lat_plot_cb(struct lws_context * context,const lws_detlat_t * d)50 lws_det_lat_plot_cb(struct lws_context *context, const lws_detlat_t *d)
51 {
52 char buf[80], *p = buf, *end = &p[sizeof(buf) - 1];
53
54 if (!context->detailed_latency_filepath)
55 return 1;
56
57 if (context->latencies_fd == -1) {
58 context->latencies_fd = open(context->detailed_latency_filepath,
59 LWS_O_CREAT | LWS_O_TRUNC | LWS_O_WRONLY, 0600);
60 if (context->latencies_fd == -1)
61 return 1;
62 }
63
64 p += lws_snprintf(p, lws_ptr_diff(end, p),
65 "%llu %c %u %u %u %u %u %zu %zu\n",
66 (unsigned long long)lws_now_usecs(), types[d->type],
67 d->latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE],
68 d->latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX],
69 d->latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE],
70 d->latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE] +
71 d->latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX] +
72 d->latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE],
73 d->latencies[LAT_DUR_PROXY_RX_TO_ONWARD_TX],
74 d->acc_size, d->req_size);
75
76 write(context->latencies_fd, buf, lws_ptr_diff(p, buf));
77
78 return 0;
79 }
80