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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**@file stats.h
  20. * @ingroup monitoring */
  21. #ifndef _STATS__H___
  22. #define _STATS__H___
  23. #include "config.h"
  24. #include <fcgiapp.h>
  25. #include <stdarg.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #define PYFCGI_STATS_SZ (900)
  29. #define PYFCGI_STATS_SIGALRM 30
  30. typedef struct pyfcgi_stats_s pyfcgi_stats_t;
  31. typedef struct pyfcgi_stats_sample_s pyfcgi_stats_sample_t;
  32. typedef struct pyfcgi_stats_shm_s pyfcgi_stats_shm_t;
  33. #include "conf.h"
  34. #include "logger.h"
  35. /**@brief Stores data for stats on 15min (900s) */
  36. struct pyfcgi_stats_sample_s
  37. {
  38. /**@brief One sample per seconds */
  39. int samples[PYFCGI_STATS_SZ];
  40. /**@brief Current sample */
  41. int cur;
  42. };
  43. struct pyfcgi_stats_s
  44. {
  45. /**@brief Request per seconds on 15 minutes */
  46. pyfcgi_stats_sample_t reqs;
  47. /**@brief Worker count */
  48. pyfcgi_stats_sample_t wcount;
  49. /**@brief Load */
  50. pyfcgi_stats_sample_t load;
  51. /**@brief Repeating 1s timer sending SIGALRM */
  52. timer_t timerid;
  53. /**@brief Old SIGALRM handler */
  54. struct sigaction oldact;
  55. /**@brief Buffer to format the statistics before sending them */
  56. char *buff;
  57. size_t buff_ptr;
  58. size_t buff_len;
  59. };
  60. struct pyfcgi_stats_shm_s
  61. {
  62. int nworker;
  63. int pool_load;
  64. };
  65. /**@brief Starts collecting statistics
  66. *
  67. * Set an handler for SIGALRM and set a repeating alarm each seconds
  68. * @warning designed to be called from monitor server process
  69. * @return -1 on error else 0
  70. */
  71. int pyfcgi_stats_init();
  72. /**@brief SIGALRM signal handler
  73. * @param int signum
  74. */
  75. void pyfcgi_stats_collector(int);
  76. /**@brief Format all stats in a buffer
  77. * @note uses the pyfcgi_stats.statsbuff buffer */
  78. size_t pyfcgi_stats_format();
  79. /**@brief Print in buffer using buff_ptr index
  80. *@note exit on mem alloc failure
  81. */
  82. void pyfcgi_stats_buffprintf(const char *fmt, ...);
  83. /**@brief Allocate the buffer with at least given space
  84. * @param size_t sz the minimum buffer size
  85. * @note allocation size is rounded
  86. * @return 0 if OK else -1
  87. */
  88. int pyfcgi_stats_reqbuff(size_t);
  89. /**@brief Returns statistics about a 900s ringbuffer
  90. *
  91. * @param const int[PYFCGI_STATS_SZ] data : a 900 ringbuffer stats samples
  92. * @param int* idx_nxt next data index in ring buffer (allow interrupt
  93. * detection : when *idx_nxt changes)
  94. * @param int *last : last sample
  95. * @param double[4] avgs : average samples on 1, 5, 10 and 15 minutes
  96. * @return 0 if no error else -1 and set errno to EINTR
  97. */
  98. int pyfcgi_stats_avg(const int[PYFCGI_STATS_SZ], int*, int*, double[4]);
  99. /**@brief Same than @ref pyfcgi_stats_avg but normalize average when
  100. * uptime is bellow 900
  101. * @see pyfcgi_stats_avg
  102. */
  103. int pyfcgi_stats_avg_const(const int[PYFCGI_STATS_SZ], int*, int*, double[4]);
  104. /**@brief Returns the formated buffer
  105. * @todo make @ref pyfcgi_stats_format() implement this functionnality
  106. * to remove this function... */
  107. const char *pyfcgi_stats_buff(const char **, size_t*);
  108. int pyfcgi_stats_get_shm(pyfcgi_stats_shm_t*);
  109. #endif