1/*
2 This file is part of Mtproto-proxy Library.
3
4 Mtproto-proxy Library is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 Mtproto-proxy Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright 2012-2013 Vkontakte Ltd
18 2012-2013 Anton Maydell
19
20 Copyright 2014-2017 Telegram Messenger Inc
21 2014-2017 Anton Maydell
22*/
23
24#pragma once
25
26#include <string.h>
27#include <sys/types.h>
28#include <stdio.h>
29
30#define AM_GET_MEMORY_USAGE_SELF 1
31#define AM_GET_MEMORY_USAGE_OVERALL 2
32
33#define SB_PRINT_I64(x) sb_printf (&sb, "%s\t%lld\n", #x, x)
34#define SB_PRINT_I32(x) sb_printf (&sb, "%s\t%d\n", #x, x)
35#define SB_PRINT_QUERIES(x) sb_print_queries (&sb, #x, x)
36#define SB_PRINT_DOUBLE(x) sb_printf (&sb, "%s\t%.6lf\n", #x, x)
37#define SB_PRINT_TIME(x) sb_printf (&sb, "%s\t%.6lfs\n", #x, x)
38#define SB_PRINT_PERCENT(x) sb_printf (&sb, "%s\t%.3lf%%\n", #x, x)
39
40#define SBP_PRINT_I32(x) sb_printf (sb, "%s\t%d\n", #x, x)
41#define SBP_PRINT_I64(x) sb_printf (sb, "%s\t%lld\n", #x, x)
42#define SBP_PRINT_QUERIES(x) sb_print_queries (sb, #x, x)
43#define SBP_PRINT_DOUBLE(x) sb_printf (sb, "%s\t%.6lf\n", #x, x)
44#define SBP_PRINT_TIME(x) sb_printf (sb, "%s\t%.6lfs\n", #x, x)
45#define SBP_PRINT_PERCENT(x) sb_printf (sb, "%s\t%.3lf%%\n", #x, x)
46#define SBP_PRINT_DATE(x) sbp_print_date (sb, #x, x)
47
48#define SBM_PRINT_I32(x) sb_printf (sb, "%s%s\t%d\n", MODULE_STAT_PREFIX_NAME ?: "", #x, x)
49#define SBM_PRINT_I64(x) sb_printf (sb, "%s%s\t%lld\n", MODULE_STAT_PREFIX_NAME ?: "", #x, x)
50#define SBM_PRINT_DOUBLE(x) sb_printf (sb, "%s%s\t%.6lf\n", MODULE_STAT_PREFIX_NAME ?: "", #x, x)
51#define SBM_PRINT_TIME(x) sb_printf (sb, "%s%s\t%.6lfs\n", MODULE_STAT_PREFIX_NAME ?: "", #x, x)
52#define SBM_PRINT_PERCENT(x) sb_printf (sb, "%s%s\t%.3lf%%\n", MODULE_STAT_PREFIX_NAME ?: "", #x, x)
53
54static inline double safe_div (double x, double y) { return y > 0 ? x/y : 0; }
55
56typedef struct {
57 long long vm_size;
58 long long vm_rss;
59 long long vm_data;
60 long long mem_free;
61 long long swap_total;
62 long long swap_free;
63 long long swap_used;
64 long long mem_cached;
65} am_memory_stat_t;
66
67int am_get_memory_usage (pid_t pid, long long *a, int m);
68int am_get_memory_stats (am_memory_stat_t *S, int flags);
69
70typedef struct stats_buffer {
71 char *buff;
72 int pos;
73 int size;
74 int flags;
75} stats_buffer_t;
76
77void sb_init (stats_buffer_t *sb, char *buff, int size);
78void sb_alloc (stats_buffer_t *sb, int size);
79void sb_release (stats_buffer_t *sb);
80
81void sb_prepare (stats_buffer_t *sb);
82void sb_printf (stats_buffer_t *sb, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
83void sb_memory (stats_buffer_t *sb, int flags);
84void sb_print_queries (stats_buffer_t *sb, const char *const desc, long long q);
85void sbp_print_date (stats_buffer_t *sb, const char *key, time_t unix_time);
86
87typedef void (*stat_fun_t) (stats_buffer_t *sb);
88int sb_register_stat_fun (stat_fun_t fun);
89
90int sb_sum_i (void **base, int len, int offset);
91long long sb_sum_ll (void **base, int len, int offset);
92double sb_sum_f (void **base, int len, int offset);
93
94#define SB_SUM_I(name) \
95 sb_sum_i ((void **)MODULE_STAT_ARR, max_job_thread_id + 1, offsetof (MODULE_STAT_TYPE, name))
96
97#define SB_SUM_LL(name) \
98 sb_sum_ll ((void **)MODULE_STAT_ARR, max_job_thread_id + 1, offsetof (MODULE_STAT_TYPE, name))
99
100#define SB_SUM_F(name) \
101 sb_sum_f ((void **)MODULE_STAT_ARR, max_job_thread_id + 1, offsetof (MODULE_STAT_TYPE, name))
102
103#define SB_SUM_ONE_I(name) sb_printf (sb, "%s%s\t%d\n", MODULE_STAT_PREFIX_NAME ?: "", #name, SB_SUM_I(name))
104#define SB_SUM_ONE_LL(name) sb_printf (sb, "%s%s\t%lld\n", MODULE_STAT_PREFIX_NAME ?: "", #name, SB_SUM_LL(name))
105#define SB_SUM_ONE_F(name) sb_printf (sb, "%s%s\t%lf\n", MODULE_STAT_PREFIX_NAME ?: "", #name, SB_SUM_F(name))
106