Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stats.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2019 Weber Yann
  3. *
  4. * This file is part of PyFCGI.
  5. *
  6. * PyFCGI is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * PyFCGI 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 Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**@defgroup stats PyFCGI statistics
  20. * @brief Information about statistics collection mechanism
  21. * @ingroup monitor_proc
  22. */
  23. /**@file stats.h
  24. * @ingroup monitoring */
  25. #ifndef _STATS__H___
  26. #define _STATS__H___
  27. #include "config.h"
  28. #include <fcgiapp.h>
  29. #include <stdarg.h>
  30. #include <signal.h>
  31. #include <string.h>
  32. /**@brief Stats history size (900 = 900s = 15min) */
  33. #define PYFCGI_STATS_SZ (900)
  34. /**@brief Signal used to wake up stats server in order to collect stats*/
  35. #define PYFCGI_STATS_SIGALRM 30
  36. typedef struct pyfcgi_stats_s pyfcgi_stats_t;
  37. typedef struct pyfcgi_stats_sample_s pyfcgi_stats_sample_t;
  38. typedef struct pyfcgi_stats_shm_s pyfcgi_stats_shm_t;
  39. #include "conf.h"
  40. #include "logger.h"
  41. /**@brief Stores data for stats on 15min (900s)
  42. * @ingroup stats
  43. */
  44. struct pyfcgi_stats_sample_s
  45. {
  46. /**@brief One sample per seconds */
  47. int samples[PYFCGI_STATS_SZ];
  48. /**@brief Current sample */
  49. int cur;
  50. };
  51. /**@brief Stores statistics informations
  52. * @ingroup stats
  53. */
  54. struct pyfcgi_stats_s
  55. {
  56. /**@brief Request per seconds on 15 minutes */
  57. pyfcgi_stats_sample_t reqs;
  58. /**@brief Worker count */
  59. pyfcgi_stats_sample_t wcount;
  60. /**@brief Load */
  61. pyfcgi_stats_sample_t load;
  62. /**@brief Repeating 1s timer sending SIGALRM */
  63. timer_t timerid;
  64. /**@brief Old SIGALRM handler */
  65. struct sigaction oldact;
  66. /**@brief Buffer to format the statistics before sending them */
  67. char *buff;
  68. size_t buff_ptr;
  69. size_t buff_len;
  70. };
  71. /**@brief Stores stats information on worker count & load average
  72. * @note Data are updated by @ref work_master_proc
  73. */
  74. struct pyfcgi_stats_shm_s
  75. {
  76. /**@brief Workers count */
  77. int nworker;
  78. /**@brief Load average */
  79. int pool_load;
  80. };
  81. /**@brief Starts collecting statistics
  82. *
  83. * Set an handler for SIGALRM and set a repeating alarm each seconds
  84. * @warning designed to be called from monitor server process
  85. * @return -1 on error else 0
  86. * @ingroup stats
  87. */
  88. int pyfcgi_stats_init();
  89. /**@brief SIGALRM signal handler
  90. * @param signum
  91. */
  92. void pyfcgi_stats_collector(int signum);
  93. /**@brief Format all stats in a buffer
  94. * @note uses the pyfcgi_stats.statsbuff buffer */
  95. size_t pyfcgi_stats_format();
  96. /**@brief Print in buffer using buff_ptr index
  97. *@note exit on mem alloc failure
  98. */
  99. void pyfcgi_stats_buffprintf(const char *fmt, ...);
  100. /**@brief Allocate the buffer with at least given space
  101. * @param sz the minimum buffer size
  102. * @note allocation size is rounded
  103. * @return 0 if OK else -1
  104. */
  105. int pyfcgi_stats_reqbuff(size_t sz);
  106. /**@brief Returns statistics about a 900s ringbuffer
  107. *
  108. * @param data a 900 ringbuffer stats samples
  109. * @param idx_nxt next data index in ring buffer (allow interrupt
  110. * detection : when *idx_nxt changes)
  111. * @param *last : last sample
  112. * @param avgs : average samples on 1, 5, 10 and 15 minutes
  113. * @return 0 if no error else -1 and set errno to EINTR
  114. */
  115. int pyfcgi_stats_avg(const int data[PYFCGI_STATS_SZ], int* idx_nxt, int* last,
  116. double avgs[4]);
  117. /**@brief Same than @ref pyfcgi_stats_avg but normalize average when
  118. * uptime is bellow 900
  119. * @see pyfcgi_stats_avg
  120. * @param data a 900 ringbuffer stats samples
  121. * @param idx_nxt next data index in ring buffer (allow interrupt
  122. * detection : when *idx_nxt changes)
  123. * @param *last : last sample
  124. * @param avgs : average samples on 1, 5, 10 and 15 minutes
  125. * @return 0 if no error else -1 and set errno to EINTR
  126. */
  127. int pyfcgi_stats_avg_const(const int data[PYFCGI_STATS_SZ], int* idx_nxt, int* last,
  128. double avgs[4]);
  129. /**@brief Returns the formated buffer
  130. * @todo make @ref pyfcgi_stats_format() implement this functionnality
  131. * to remove this function... */
  132. const char *pyfcgi_stats_buff(const char **, size_t*);
  133. /**@brief Fetch stats informations from dedicated SHM
  134. * @see pyfcgi_stats_shm_s
  135. */
  136. int pyfcgi_stats_get_shm(pyfcgi_stats_shm_t*);
  137. #endif