/src/h2o/lib/handler/status/memory.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022 Fastly |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | |
23 | | #include "h2o.h" |
24 | | #include <inttypes.h> |
25 | | |
26 | | struct st_recycle_status_t { |
27 | | uint64_t chunks; |
28 | | uint64_t low_watermark; |
29 | | }; |
30 | | |
31 | | struct st_memory_status_ctx_t { |
32 | | pthread_mutex_t mutex; |
33 | | struct st_recycle_status_t mem_pool, socket_ssl, socket_zerocopy; |
34 | | size_t socket_zerocopy_inflight; |
35 | | }; |
36 | | |
37 | | static void recycle_status_per_thread(struct st_recycle_status_t *status, h2o_mem_recycle_t *recycle) |
38 | 0 | { |
39 | 0 | status->chunks += recycle->chunks.size; |
40 | 0 | status->low_watermark += recycle->low_watermark; |
41 | 0 | } |
42 | | |
43 | | static void memory_status_per_thread(void *priv, h2o_context_t *ctx) |
44 | 0 | { |
45 | 0 | struct st_memory_status_ctx_t *csc = priv; |
46 | |
|
47 | 0 | pthread_mutex_lock(&csc->mutex); |
48 | |
|
49 | 0 | recycle_status_per_thread(&csc->mem_pool, &h2o_mem_pool_allocator); |
50 | 0 | recycle_status_per_thread(&csc->socket_ssl, &h2o_socket_ssl_buffer_allocator); |
51 | 0 | recycle_status_per_thread(&csc->socket_zerocopy, &h2o_socket_zerocopy_buffer_allocator); |
52 | 0 | csc->socket_zerocopy_inflight += h2o_socket_num_zerocopy_buffers_inflight; |
53 | |
|
54 | 0 | pthread_mutex_unlock(&csc->mutex); |
55 | 0 | } |
56 | | |
57 | | static void *memory_status_init(void) |
58 | 0 | { |
59 | 0 | struct st_memory_status_ctx_t *ret = h2o_mem_alloc(sizeof(*ret)); |
60 | 0 | *ret = (struct st_memory_status_ctx_t){PTHREAD_MUTEX_INITIALIZER}; |
61 | 0 | return ret; |
62 | 0 | } |
63 | | |
64 | | static h2o_iovec_t memory_status_json(void *priv, h2o_globalconf_t *gconf, h2o_req_t *req) |
65 | 0 | { |
66 | 0 | struct st_memory_status_ctx_t *csc = priv; |
67 | 0 | h2o_iovec_t ret; |
68 | |
|
69 | 0 | #define BUFSIZE 512 |
70 | 0 | #define FMT(prefix) \ |
71 | 0 | " \"memory." H2O_TO_STR(prefix) ".chunks\": %" PRIu64 ",\n \"memory." H2O_TO_STR(prefix) ".low_watermark\": %" PRIu64 ",\n" |
72 | 0 | #define ARGS(prefix) csc->prefix.chunks, csc->prefix.low_watermark |
73 | 0 | ret.base = h2o_mem_alloc_pool(&req->pool, char, BUFSIZE); |
74 | 0 | ret.len = snprintf(ret.base, BUFSIZE, |
75 | 0 | ",\n" FMT(mem_pool) FMT(socket.ssl) FMT(socket.zerocopy) " \"memory.socket.zerocopy.inflight\": %zu\n", |
76 | 0 | ARGS(mem_pool), ARGS(socket_ssl), ARGS(socket_zerocopy), |
77 | 0 | csc->socket_zerocopy_inflight * h2o_socket_zerocopy_buffer_allocator.conf->memsize); |
78 | 0 | #undef FMT |
79 | 0 | #undef ARGS |
80 | 0 | #undef BUFSIZE |
81 | |
|
82 | 0 | pthread_mutex_destroy(&csc->mutex); |
83 | 0 | free(csc); |
84 | 0 | return ret; |
85 | 0 | } |
86 | | |
87 | | h2o_status_handler_t h2o_memory_status_handler = { |
88 | | {H2O_STRLIT("memory")}, |
89 | | memory_status_json, |
90 | | memory_status_init, |
91 | | memory_status_per_thread, |
92 | | }; |