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.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "stats.h"
  2. static pyfcgi_stats_t pyfcgi_stats;
  3. int pyfcgi_stats_init()
  4. {
  5. struct sigaction act;
  6. struct itimerspec timeout;
  7. memset(&pyfcgi_stats, 0, sizeof(pyfcgi_stats_t));
  8. if(timer_create(CLOCK_REALTIME, NULL, &(pyfcgi_stats.timerid)) < 0)
  9. {
  10. pyfcgi_log(LOG_ERR,
  11. "Unable to create timer for stats collecting : %s",
  12. strerror(errno));
  13. goto err;
  14. }
  15. act.sa_handler = pyfcgi_stats_collector;
  16. sigemptyset(&act.sa_mask);
  17. act.sa_flags = 0;
  18. act.sa_restorer = NULL;
  19. if(sigaction(SIGALRM, &act, &(pyfcgi_stats.oldact)) < 0)
  20. {
  21. pyfcgi_log(LOG_ERR,
  22. "Unable to register signal handler for stats collecting : %s",
  23. strerror(errno));
  24. goto err_deltimer;
  25. }
  26. timeout.it_value.tv_sec = 1;
  27. timeout.it_value.tv_nsec = 0;
  28. timeout.it_interval = timeout.it_value;
  29. if(timer_settime(pyfcgi_stats.timerid, 0, &timeout, NULL) < 0)
  30. {
  31. pyfcgi_log(LOG_ERR,
  32. "Unable to start timer for stats collecting : %s",
  33. strerror(errno));
  34. goto err_sigrestore;
  35. }
  36. return 0;
  37. err_sigrestore:
  38. sigaction(SIGALRM, &(pyfcgi_stats.oldact), NULL);
  39. err_deltimer:
  40. timer_delete(pyfcgi_stats.timerid);
  41. err:
  42. memset(&pyfcgi_stats, 0, sizeof(pyfcgi_stats_t));
  43. return -1;
  44. }
  45. void pyfcgi_stats_collector(int signum)
  46. {
  47. int ret;
  48. pyfcgi_stats_t *stats;
  49. stats = &pyfcgi_stats;
  50. stats->reqs[stats->cur_req] = 0;
  51. while( !(ret = sem_trywait(PyFCGI_SEM(SEM_WREQS).sem)) )
  52. {
  53. stats->reqs[stats->cur_req]++;
  54. }
  55. pyfcgi_log(LOG_DEBUG, "%d req/s", stats->reqs[stats->cur_req]);
  56. stats->cur_req++;
  57. return;
  58. }