Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/pt_load.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2018 OpenSIPS Solutions
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
22
#include <string.h>
23
#include <sys/time.h>
24
#include <stdio.h>
25
26
#include "dprint.h"
27
#include "pt_load.h"
28
#include "pt.h"
29
#include "ut.h"
30
31
0
#define PT_LOAD(_pno)    pt[_pno].load
32
33
0
#define MY_LOAD          PT_LOAD(process_no)
34
35
#define FOR_ALL_INDEXES(_it, _old, _new, _TYPE) \
36
0
  for( _it=(_old+1)%_TYPE##_WINDOW_SIZE ; _it!=_new ;\
37
0
    _it=(_it+1)%_TYPE##_WINDOW_SIZE )
38
39
40
#define MARK_AS_IDLE( _now, _TYPE) \
41
0
  do { \
42
0
    /* check if entire time window was idle */ \
43
0
    if (_now-MY_LOAD.last_time >= _TYPE##_WINDOW_TIME) { \
44
0
      /* all was idle, so make it zero */ \
45
0
      MY_LOAD._TYPE##_window[0] = 0; \
46
0
      FOR_ALL_INDEXES( i, 0, 0, _TYPE) \
47
0
        MY_LOAD._TYPE##_window[i] = 0; \
48
0
    } else { \
49
0
      /* get the index inside window for the last time update */ \
50
0
      idx_old = (MY_LOAD.last_time / _TYPE##_WINDOW_UNIT) % \
51
0
        _TYPE##_WINDOW_SIZE; \
52
0
      idx_new = (_now / _TYPE##_WINDOW_UNIT) % _TYPE##_WINDOW_SIZE; \
53
0
      if (idx_old!=idx_new) { \
54
0
        FOR_ALL_INDEXES( i, idx_old, idx_new, _TYPE) { \
55
0
          MY_LOAD._TYPE##_window[i] = 0; \
56
0
        } \
57
0
        MY_LOAD._TYPE##_window[idx_new] = 0; \
58
0
      }\
59
0
    } \
60
0
  }while(0)
61
62
63
void pt_become_active(void)
64
0
{
65
0
  utime_t usec_now;
66
0
  struct timeval tv;
67
0
  int idx_old, idx_new, i;
68
69
0
  gettimeofday( &tv, NULL);
70
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
71
0
  if (usec_now==MY_LOAD.last_time) {
72
0
    __atomic_store_n(&MY_LOAD.is_busy, 1, __ATOMIC_RELEASE);
73
0
    return;
74
0
  }
75
76
0
  MARK_AS_IDLE( usec_now, ST);
77
0
  MARK_AS_IDLE( usec_now, LT);
78
79
0
  __atomic_store_n(&MY_LOAD.last_time, usec_now, __ATOMIC_RELAXED);
80
  /* release: a reader seeing the new is_busy also sees the new last_time */
81
0
  __atomic_store_n(&MY_LOAD.is_busy, 1, __ATOMIC_RELEASE);
82
0
}
83
84
85
#define MARK_AS_ACTIVE( _now, _TYPE) \
86
0
  do { \
87
0
    /* check if the entire time window was active */ \
88
0
    if ((_now-MY_LOAD.last_time) >= _TYPE##_WINDOW_TIME) { \
89
0
      /* all was busy, so make it "full" */ \
90
0
      MY_LOAD._TYPE##_window[0] = _TYPE##_WINDOW_UNIT; \
91
0
      FOR_ALL_INDEXES( i, 0, 0, _TYPE) \
92
0
        MY_LOAD._TYPE##_window[i] = _TYPE##_WINDOW_UNIT; \
93
0
    } else { \
94
0
      /* get the index inside window for the last time update */ \
95
0
      idx_old = (MY_LOAD.last_time / _TYPE##_WINDOW_UNIT) % \
96
0
        _TYPE##_WINDOW_SIZE; \
97
0
      idx_new = (_now / _TYPE##_WINDOW_UNIT) % \
98
0
        _TYPE##_WINDOW_SIZE; \
99
0
      if (idx_old!=idx_new) { \
100
0
        /* do partial update on the last used index */ \
101
0
        MY_LOAD._TYPE##_window[idx_old] += _TYPE##_WINDOW_UNIT - \
102
0
          (MY_LOAD.last_time % _TYPE##_WINDOW_UNIT); \
103
0
        /* update the fully used */ \
104
0
        FOR_ALL_INDEXES( i, idx_old, idx_new, _TYPE) { \
105
0
          MY_LOAD._TYPE##_window[i] = _TYPE##_WINDOW_UNIT; \
106
0
        } \
107
0
        /* do partial update on the last used index */ \
108
0
        MY_LOAD._TYPE##_window[i] = _now % _TYPE##_WINDOW_UNIT; \
109
0
      } else { \
110
0
        MY_LOAD._TYPE##_window[idx_old] += \
111
0
          (_now % _TYPE##_WINDOW_UNIT) \
112
0
           - \
113
0
          (MY_LOAD.last_time % _TYPE##_WINDOW_UNIT); \
114
0
      } \
115
0
    } \
116
0
  }while(0)
117
118
void pt_become_idle(void)
119
0
{
120
0
  utime_t usec_now;
121
0
  struct timeval tv;
122
0
  int idx_old, idx_new, i;
123
124
0
  gettimeofday( &tv, NULL);
125
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
126
0
  if (usec_now==MY_LOAD.last_time) {
127
0
    __atomic_store_n(&MY_LOAD.is_busy, 0, __ATOMIC_RELEASE);
128
0
    return;
129
0
  }
130
131
0
  MARK_AS_ACTIVE( usec_now, ST);
132
0
  MARK_AS_ACTIVE( usec_now, LT);
133
134
0
  __atomic_store_n(&MY_LOAD.last_time, usec_now, __ATOMIC_RELAXED);
135
  /* release: a reader seeing the new is_busy also sees the new last_time */
136
0
  __atomic_store_n(&MY_LOAD.is_busy, 0, __ATOMIC_RELEASE);
137
0
}
138
139
140
#define SUM_UP_LOAD(_now, _pno, _TYPE, _ratio) \
141
0
  do { \
142
0
    /* The process updates its load info without any lock, so it may \
143
0
     * do so while we are reading it. Take a snapshot: is_busy first \
144
0
     * (acquire, pairs with the release store in pt_become_*()), then \
145
0
     * last_time. And as the update may be newer than our own "_now" \
146
0
     * sample, never evaluate it at a time before that update - \
147
0
     * otherwise idx_old ends up ahead of idx_new and the loops below \
148
0
     * wrap around the whole window, accounting it all as busy. */ \
149
0
    unsigned char __busy = \
150
0
      __atomic_load_n(&PT_LOAD(_pno).is_busy, __ATOMIC_ACQUIRE); \
151
0
    utime_t __last = \
152
0
      __atomic_load_n(&PT_LOAD(_pno).last_time, __ATOMIC_RELAXED); \
153
0
    utime_t __now = ((utime_t)(_now) > __last) ? (utime_t)(_now) : __last; \
154
0
    /* check if the entire time window has the same status */ \
155
0
    if (((long long)__now-(long long)__last) >= \
156
0
        (_TYPE##_WINDOW_TIME)*(_ratio)){\
157
0
      /* nothing recorded in the last time window */ \
158
0
      used += __busy?_TYPE##_WINDOW_TIME*_ratio:0; \
159
0
    } else { \
160
0
      /* get the index inside window for the last time update */ \
161
0
      idx_old = (__last / _TYPE##_WINDOW_UNIT) % \
162
0
        _TYPE##_WINDOW_SIZE; \
163
0
      idx_new = (__now / _TYPE##_WINDOW_UNIT) % \
164
0
        _TYPE##_WINDOW_SIZE; \
165
0
      /* ajust the index where we start counting the past used-time \
166
0
       * based on the "ratio" option, if present */  \
167
0
      if (_ratio!=1) { \
168
0
        idx_start = (idx_new+(int)(_TYPE##_WINDOW_SIZE*(1-_ratio))) % \
169
0
          _TYPE##_WINDOW_SIZE; \
170
0
        /* The last update shares the window's first slot. Only \
171
0
         * the part of that slot inside the window can count. */ \
172
0
        if (idx_start == idx_old) { \
173
0
          utime_t elapsed = __now - __last; \
174
0
          utime_t overlap = (utime_t)(_TYPE##_WINDOW_TIME * (_ratio)) - elapsed; \
175
0
          utime_t recorded = PT_LOAD(_pno)._TYPE##_window[idx_old]; \
176
0
          used += recorded < overlap ? recorded : overlap; \
177
0
          if (__busy) \
178
0
            used += elapsed; \
179
0
          break; \
180
0
        }\
181
0
      } else { \
182
0
        idx_start = idx_new; \
183
0
      }\
184
0
      /* sum up the already accounted used time */ \
185
0
      FOR_ALL_INDEXES( i, idx_start, idx_old, _TYPE) { \
186
0
        used += PT_LOAD(_pno)._TYPE##_window[i]; \
187
0
      } \
188
0
      /* add what is not accounted since last update */ \
189
0
      if (__busy) { \
190
0
        if (idx_old!=idx_new) { \
191
0
          /* count the last used index (existing + new) */ \
192
0
          used += PT_LOAD(_pno)._TYPE##_window[idx_old] + \
193
0
            (_TYPE##_WINDOW_UNIT - \
194
0
            (__last % _TYPE##_WINDOW_UNIT)); \
195
0
          /* update the fully used */ \
196
0
          FOR_ALL_INDEXES( i, idx_old, idx_new, _TYPE) { \
197
0
            used += _TYPE##_WINDOW_UNIT; \
198
0
          } \
199
0
          /* do partial update on current index */ \
200
0
          used += __now % _TYPE##_WINDOW_UNIT; \
201
0
        } else { \
202
0
          used += \
203
0
            (__now % _TYPE##_WINDOW_UNIT) \
204
0
             - \
205
0
            (__last % _TYPE##_WINDOW_UNIT); \
206
0
        } \
207
0
      } \
208
0
    } \
209
0
  } while(0)
210
211
unsigned int pt_get_rt_proc_load( int pno )
212
0
{
213
0
  utime_t usec_now;
214
0
  struct timeval tv;
215
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
216
0
  unsigned long long used = 0;
217
218
0
  gettimeofday( &tv, NULL);
219
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
220
221
0
  SUM_UP_LOAD( usec_now, pno, ST, 1);
222
223
0
  return (used*100/ST_WINDOW_TIME);
224
0
}
225
226
227
unsigned int pt_get_1m_proc_load( int pno )
228
0
{
229
0
  utime_t usec_now;
230
0
  struct timeval tv;
231
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
232
0
  unsigned long long used = 0;
233
234
0
  gettimeofday( &tv, NULL);
235
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
236
237
0
  SUM_UP_LOAD( usec_now, pno, LT, LT_1m_RATIO);
238
239
0
  return (used*100/(LT_WINDOW_TIME*LT_1m_RATIO));
240
0
}
241
242
243
unsigned int pt_get_10m_proc_load( int pno )
244
0
{
245
0
  utime_t usec_now;
246
0
  struct timeval tv;
247
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
248
0
  unsigned long long used = 0;
249
250
0
  gettimeofday( &tv, NULL);
251
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
252
253
0
  SUM_UP_LOAD( usec_now, pno, LT, 1);
254
255
0
  return (used*100/LT_WINDOW_TIME);
256
0
}
257
258
259
unsigned int pt_get_rt_load(int _)
260
0
{
261
0
  utime_t usec_now;
262
0
  struct timeval tv;
263
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
264
0
  unsigned int n, summed_procs=0;
265
0
  unsigned long long used = 0;
266
267
0
  gettimeofday( &tv, NULL);
268
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
269
270
0
  for( n=0 ; n<counted_max_processes; n++)
271
0
    if ( is_process_running(n) &&
272
0
    (pt[n].flags&(OSS_PROC_NO_LOAD|OSS_PROC_IS_EXTRA))==0 ) {
273
0
      SUM_UP_LOAD( usec_now, n, ST, 1);
274
0
      summed_procs++;
275
0
    }
276
0
  if (!summed_procs)
277
0
    return 0;
278
279
0
  return (used*100/(ST_WINDOW_TIME*summed_procs));
280
0
}
281
282
283
unsigned int pt_get_1m_load(int _)
284
0
{
285
0
  utime_t usec_now;
286
0
  struct timeval tv;
287
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
288
0
  unsigned int n, summed_procs=0;
289
0
  unsigned long long used = 0;
290
291
0
  gettimeofday( &tv, NULL);
292
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
293
294
0
  for( n=0 ; n<counted_max_processes; n++)
295
0
    if ( is_process_running(n) &&
296
0
    (pt[n].flags&(OSS_PROC_NO_LOAD|OSS_PROC_IS_EXTRA))==0 ) {
297
0
      SUM_UP_LOAD( usec_now, n, LT, LT_1m_RATIO);
298
0
      summed_procs++;
299
0
    }
300
0
  if (!summed_procs)
301
0
    return 0;
302
303
0
  return (used*100/((long long)LT_WINDOW_TIME*summed_procs*LT_1m_RATIO));
304
0
}
305
306
307
unsigned int pt_get_10m_load(int _)
308
0
{
309
0
  utime_t usec_now;
310
0
  struct timeval tv;
311
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
312
0
  unsigned int n, summed_procs=0;
313
0
  unsigned long long used = 0;
314
315
0
  gettimeofday( &tv, NULL);
316
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
317
318
0
  for( n=0 ; n<counted_max_processes; n++)
319
0
    if ( is_process_running(n) &&
320
0
    (pt[n].flags&(OSS_PROC_NO_LOAD|OSS_PROC_IS_EXTRA))==0 ) {
321
0
      SUM_UP_LOAD( usec_now, n, LT, 1);
322
0
      summed_procs++;
323
0
    }
324
0
  if (!summed_procs)
325
0
    return 0;
326
327
0
  return (used*100/((long long)LT_WINDOW_TIME*summed_procs));
328
0
}
329
330
331
unsigned int pt_get_rt_loadall(int _)
332
0
{
333
0
  utime_t usec_now;
334
0
  struct timeval tv;
335
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
336
0
  unsigned int n, summed_procs=0;
337
0
  unsigned long long used = 0;
338
339
0
  gettimeofday( &tv, NULL);
340
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
341
342
0
  for( n=0 ; n<counted_max_processes; n++)
343
0
    if ( is_process_running(n) && (pt[n].flags&OSS_PROC_NO_LOAD)==0 ) {
344
0
      SUM_UP_LOAD( usec_now, n, ST, 1);
345
0
      summed_procs++;
346
0
    }
347
0
  if (!summed_procs)
348
0
    return 0;
349
350
0
  return (used*100/((long long)ST_WINDOW_TIME*summed_procs));
351
0
}
352
353
354
unsigned int pt_get_1m_loadall(int _)
355
0
{
356
0
  utime_t usec_now;
357
0
  struct timeval tv;
358
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
359
0
  unsigned int n, summed_procs=0;
360
0
  unsigned long long used = 0;
361
362
0
  gettimeofday( &tv, NULL);
363
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
364
365
0
  for( n=0 ; n<counted_max_processes; n++)
366
0
    if ( is_process_running(n) && (pt[n].flags&OSS_PROC_NO_LOAD)==0 ) {
367
0
      SUM_UP_LOAD( usec_now, n, LT, LT_1m_RATIO);
368
0
      summed_procs++;
369
0
    }
370
0
  if (!summed_procs)
371
0
    return 0;
372
373
0
  return (used*100/((long long)LT_WINDOW_TIME*summed_procs*LT_1m_RATIO));
374
0
}
375
376
377
unsigned int pt_get_10m_loadall(int _)
378
0
{
379
0
  utime_t usec_now;
380
0
  struct timeval tv;
381
0
  int idx_old, idx_new, idx_start, i; /* used inside the macro */
382
0
  unsigned int n, summed_procs=0;
383
0
  unsigned long long used = 0;
384
385
0
  gettimeofday( &tv, NULL);
386
0
  usec_now = ((utime_t)(tv.tv_sec)) * 1000000 + tv.tv_usec;
387
388
0
  for( n=0 ; n<counted_max_processes; n++)
389
0
    if ( is_process_running(n) && (pt[n].flags&OSS_PROC_NO_LOAD)==0 ) {
390
0
      SUM_UP_LOAD( usec_now, n, LT, 1);
391
0
      summed_procs++;
392
0
    }
393
0
  if (!summed_procs)
394
0
    return 0;
395
396
0
  return (used*100/((long long)LT_WINDOW_TIME*summed_procs));
397
0
}
398
399
400
int register_processes_load_stats(int procs_no)
401
0
{
402
0
  char *stat_name;
403
0
  str stat_prefix;
404
0
  char *pno_s;
405
0
  str name;
406
0
  int pno;
407
408
0
  group_stats *load_proc_grp, *load_proc_1m_grp, *load_proc_10m_grp;
409
410
0
  load_proc_grp = register_stats_group("proc_load");
411
0
  if (!load_proc_grp) {
412
0
    LM_ERR("could not register stats group proc_load");
413
0
    return -1;
414
0
  }
415
0
  load_proc_1m_grp = register_stats_group("proc_load1m");
416
0
  if (!load_proc_1m_grp) {
417
0
    LM_ERR("could not register stats group proc_load1m");
418
0
    return -1;
419
0
  }
420
0
  load_proc_10m_grp = register_stats_group("proc_load10m");
421
0
  if (!load_proc_10m_grp) {
422
0
    LM_ERR("could not register stats group proc_load10m");
423
0
    return -1;
424
0
  }
425
426
  /* build the stats and register them for each potential process
427
   * skipp the attendant, id 0 */
428
0
  for( pno=1 ; pno<procs_no ; pno++) {
429
430
0
    pno_s = int2str( (unsigned int)pno, NULL);
431
432
0
    stat_prefix.s = "load-proc";
433
0
    stat_prefix.len = sizeof("load-proc")-1;
434
0
    if ( (stat_name = build_stat_name( &stat_prefix, pno_s)) == 0 ||
435
0
    register_stat2( "load", stat_name, (stat_var**)pt_get_rt_proc_load,
436
0
    STAT_IS_FUNC|STAT_PER_PROC, (void*)(long)pno, 0) != 0) {
437
0
      LM_ERR("failed to add RT load stat for process %d\n",pno);
438
0
    return -1;
439
0
    }
440
0
    name.s = stat_name;
441
0
    name.len = strlen(stat_name);
442
0
    pt[pno].load_rt = get_stat(&name);
443
0
    pt[pno].load_rt->flags |= STAT_HIDDEN;
444
0
    add_stats_group(load_proc_grp, pt[pno].load_rt);
445
446
0
    stat_prefix.s = "load1m-proc";
447
0
    stat_prefix.len = sizeof("load1m-proc")-1;
448
0
    if ( (stat_name = build_stat_name( &stat_prefix, pno_s)) == 0 ||
449
0
    register_stat2( "load", stat_name, (stat_var**)pt_get_1m_proc_load,
450
0
    STAT_IS_FUNC|STAT_PER_PROC, (void*)(long)pno, 0) != 0) {
451
0
      LM_ERR("failed to add RT load stat for process %d\n",pno);
452
0
      return -1;
453
0
    }
454
0
    name.s = stat_name;
455
0
    name.len = strlen(stat_name);
456
0
    pt[pno].load_1m = get_stat(&name);
457
0
    pt[pno].load_1m->flags |= STAT_HIDDEN;
458
0
    add_stats_group(load_proc_1m_grp, pt[pno].load_1m);
459
460
0
    stat_prefix.s = "load10m-proc";
461
0
    stat_prefix.len = sizeof("load10m-proc")-1;
462
0
    if ( (stat_name = build_stat_name( &stat_prefix, pno_s)) == 0 ||
463
0
    register_stat2( "load", stat_name, (stat_var**)pt_get_10m_proc_load,
464
0
    STAT_IS_FUNC|STAT_PER_PROC, (void*)(long)pno, 0) != 0) {
465
0
      LM_ERR("failed to add RT load stat for process %d\n",pno);
466
0
      return -1;
467
0
    }
468
0
    name.s = stat_name;
469
0
    name.len = strlen(stat_name);
470
0
    pt[pno].load_10m = get_stat(&name);
471
0
    pt[pno].load_10m->flags |= STAT_HIDDEN;
472
0
    add_stats_group(load_proc_10m_grp, pt[pno].load_10m);
473
0
  }
474
475
0
  return 0;
476
0
}