dmxstat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2002, 2003 Red Hat Inc., Durham, North Carolina.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation on the rights to use, copy, modify, merge,
  10. * publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. /*
  28. * Authors:
  29. * Rickard E. (Rik) Faith <faith@redhat.com>
  30. *
  31. */
  32. /** \file
  33. *
  34. * The DMX server code is written to call #dmxSync() whenever an XSync()
  35. * might be necessary. However, since XSync() requires a two way
  36. * communication with the other X server, eliminating unnecessary
  37. * XSync() calls is a key performance optimization. Support for this
  38. * optimization is provided in \a dmxsync.c. This file provides routines
  39. * that evaluate this optimization by counting the number of XSync()
  40. * calls and monitoring their latency. This functionality can be turned
  41. * on using the -stat command-line parameter. */
  42. #ifdef HAVE_DMX_CONFIG_H
  43. #include <dmx-config.h>
  44. #endif
  45. #include "dmx.h"
  46. #include "dmxstat.h"
  47. #include "dmxlog.h"
  48. #include <X11/Xos.h> /* For sys/time.h */
  49. /** Used to compute a running average of value. */
  50. typedef struct _DMXStatAvg {
  51. int pos;
  52. int count;
  53. unsigned long value[DMX_STAT_LENGTH];
  54. } DMXStatAvg;
  55. /** Statistical information about XSync calls. */
  56. struct _DMXStatInfo {
  57. unsigned long syncCount;
  58. unsigned long oldSyncCount;
  59. DMXStatAvg usec;
  60. DMXStatAvg pending;
  61. unsigned long bins[DMX_STAT_BINS];
  62. };
  63. /* Interval in mS between statistic message log entries. */
  64. int dmxStatInterval;
  65. static int dmxStatDisplays;
  66. static OsTimerPtr dmxStatTimer;
  67. /** Return the number of microseconds as an unsigned long.
  68. * Unfortunately, this is only useful for intervals < about 4 sec. */
  69. static unsigned long
  70. usec(struct timeval *stop, struct timeval *start)
  71. {
  72. return (stop->tv_sec - start->tv_sec) * 1000000
  73. + stop->tv_usec - start->tv_usec;
  74. }
  75. static unsigned long
  76. avg(DMXStatAvg * data, unsigned long *max)
  77. {
  78. unsigned long sum;
  79. int i;
  80. *max = 0;
  81. if (!data->count)
  82. return 0;
  83. for (i = 0, sum = 0; i < data->count; i++) {
  84. if (data->value[i] > *max)
  85. *max = data->value[i];
  86. sum += data->value[i];
  87. }
  88. return sum / data->count;
  89. }
  90. /** Turn on XSync statistic gathering and printing. Print every \a
  91. * interval seconds, with lines for the first \a displays. If \a
  92. * interval is NULL, 1 will be used. If \a displays is NULL, 0 will be
  93. * used (meaning a line for every display will be printed). Note that
  94. * this function takes string arguments because it will usually be
  95. * called from #ddxProcessArgument in \a dmxinit.c. */
  96. void
  97. dmxStatActivate(const char *interval, const char *displays)
  98. {
  99. dmxStatInterval = (interval ? atoi(interval) : 1) * 1000;
  100. dmxStatDisplays = (displays ? atoi(displays) : 0);
  101. if (dmxStatInterval < 1000)
  102. dmxStatInterval = 1000;
  103. if (dmxStatDisplays < 0)
  104. dmxStatDisplays = 0;
  105. }
  106. /** Allocate a \a DMXStatInfo structure. */
  107. DMXStatInfo *
  108. dmxStatAlloc(void)
  109. {
  110. DMXStatInfo *pt = calloc(1, sizeof(*pt));
  111. return pt;
  112. }
  113. /** Free the memory used by a \a DMXStatInfo structure. */
  114. void
  115. dmxStatFree(DMXStatInfo * pt)
  116. {
  117. free(pt);
  118. }
  119. static void
  120. dmxStatValue(DMXStatAvg * data, unsigned long value)
  121. {
  122. if (data->count != DMX_STAT_LENGTH)
  123. ++data->count;
  124. if (data->pos >= DMX_STAT_LENGTH - 1)
  125. data->pos = 0;
  126. data->value[data->pos++] = value;
  127. }
  128. /** Note that a XSync() was just done on \a dmxScreen with the \a start
  129. * and \a stop times (from gettimeofday()) and the number of
  130. * pending-but-not-yet-processed XSync requests. This routine is called
  131. * from #dmxDoSync in \a dmxsync.c */
  132. void
  133. dmxStatSync(DMXScreenInfo * dmxScreen,
  134. struct timeval *stop, struct timeval *start, unsigned long pending)
  135. {
  136. DMXStatInfo *s = dmxScreen->stat;
  137. unsigned long elapsed = usec(stop, start);
  138. unsigned long thresh;
  139. int i;
  140. ++s->syncCount;
  141. dmxStatValue(&s->usec, elapsed);
  142. dmxStatValue(&s->pending, pending);
  143. for (i = 0, thresh = DMX_STAT_BIN0; i < DMX_STAT_BINS - 1; i++) {
  144. if (elapsed < thresh) {
  145. ++s->bins[i];
  146. break;
  147. }
  148. thresh *= DMX_STAT_BINMULT;
  149. }
  150. if (i == DMX_STAT_BINS - 1)
  151. ++s->bins[i];
  152. }
  153. /* Actually do the work of printing out the human-readable message. */
  154. static CARD32
  155. dmxStatCallback(OsTimerPtr timer, CARD32 t, void *arg)
  156. {
  157. int i, j;
  158. static int header = 0;
  159. int limit = dmxNumScreens;
  160. if (!dmxNumScreens) {
  161. header = 0;
  162. return DMX_STAT_INTERVAL;
  163. }
  164. if (!header++ || !(header % 10)) {
  165. dmxLog(dmxDebug,
  166. " S SyncCount Sync/s avSync mxSync avPend mxPend | "
  167. "<10ms <1s >1s\n");
  168. }
  169. if (dmxStatDisplays && dmxStatDisplays < limit)
  170. limit = dmxStatDisplays;
  171. for (i = 0; i < limit; i++) {
  172. DMXScreenInfo *dmxScreen = &dmxScreens[i];
  173. DMXStatInfo *s = dmxScreen->stat;
  174. unsigned long aSync, mSync;
  175. unsigned long aPend, mPend;
  176. if (!s)
  177. continue;
  178. aSync = avg(&s->usec, &mSync);
  179. aPend = avg(&s->pending, &mPend);
  180. dmxLog(dmxDebug, "%2d %9lu %7lu %6lu %6lu %6lu %6lu |", i, /* S */
  181. s->syncCount, /* SyncCount */
  182. (s->syncCount - s->oldSyncCount) * 1000 / dmxStatInterval, /* Sync/s */
  183. aSync, /* us/Sync */
  184. mSync, /* max/Sync */
  185. aPend, /* avgPend */
  186. mPend); /* maxPend */
  187. for (j = 0; j < DMX_STAT_BINS; j++)
  188. dmxLogCont(dmxDebug, " %5lu", s->bins[j]);
  189. dmxLogCont(dmxDebug, "\n");
  190. /* Reset/clear */
  191. s->oldSyncCount = s->syncCount;
  192. for (j = 0; j < DMX_STAT_BINS; j++)
  193. s->bins[j] = 0;
  194. }
  195. return DMX_STAT_INTERVAL; /* Place on queue again */
  196. }
  197. /** Try to initialize the statistic gathering and printing routines.
  198. * Initialization only takes place if #dmxStatActivate has already been
  199. * called. We don't need the same generation protection that we used in
  200. * dmxSyncInit because our timer is always on a queue -- hence, server
  201. * generation will always free it. */
  202. void
  203. dmxStatInit(void)
  204. {
  205. if (dmxStatInterval)
  206. dmxStatTimer = TimerSet(NULL, 0,
  207. dmxStatInterval, dmxStatCallback, NULL);
  208. }