counters.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <prcountr.h>
  42. #include "slap.h"
  43. #if defined(DEBUG)
  44. struct counter
  45. {
  46. const char *qname;
  47. const char *rname;
  48. const char *description;
  49. PRUint32 before_counter;
  50. PRUint32 after_counter;
  51. };
  52. static int num_counters= 0;
  53. static struct counter *counters= NULL;
  54. static int
  55. count_counters()
  56. {
  57. int i= 0;
  58. PR_DEFINE_COUNTER(qh);
  59. PR_INIT_COUNTER_HANDLE(qh,NULL);
  60. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  61. while(qh!=NULL)
  62. {
  63. PR_DEFINE_COUNTER(rh);
  64. PR_INIT_COUNTER_HANDLE(rh,NULL);
  65. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  66. while(rh!=NULL)
  67. {
  68. i++;
  69. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  70. }
  71. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  72. }
  73. return i;
  74. }
  75. static int
  76. do_fetch_counters()
  77. {
  78. int i= 0;
  79. PR_DEFINE_COUNTER(qh);
  80. PR_INIT_COUNTER_HANDLE(qh,NULL);
  81. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  82. while(qh!=NULL)
  83. {
  84. PR_DEFINE_COUNTER(rh);
  85. PR_INIT_COUNTER_HANDLE(rh,NULL);
  86. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  87. while(rh!=NULL)
  88. {
  89. if(i<num_counters)
  90. {
  91. counters[i].before_counter= counters[i].after_counter;
  92. PR_GET_COUNTER_NAME_FROM_HANDLE(rh,&counters[i].qname,&counters[i].rname,&counters[i].description);
  93. PR_GET_COUNTER(counters[i].after_counter,rh);
  94. }
  95. i++;
  96. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  97. }
  98. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  99. }
  100. return i;
  101. }
  102. static void
  103. fetch_counters()
  104. {
  105. int i;
  106. if(counters==NULL)
  107. {
  108. num_counters= count_counters();
  109. counters= (struct counter*)calloc(num_counters,sizeof(struct counter));
  110. }
  111. i= do_fetch_counters();
  112. if(i>num_counters)
  113. {
  114. free(counters);
  115. counters= NULL;
  116. num_counters= i;
  117. counters= (struct counter*)calloc(num_counters,sizeof(struct counter));
  118. do_fetch_counters();
  119. }
  120. }
  121. static size_t
  122. counter_size(struct counter *counter)
  123. {
  124. size_t r= 0;
  125. r+= (counter->qname?strlen(counter->qname):0);
  126. r+= (counter->rname?strlen(counter->rname):0);
  127. r+= (counter->description?strlen(counter->description):0);
  128. return r;
  129. }
  130. static void
  131. counter_dump(char *name, char *value, int i)
  132. {
  133. PRUint32 diff_counter;
  134. sprintf(name,"%s_%s_%s",counters[i].qname,counters[i].rname,counters[i].description);
  135. diff_counter= counters[i].after_counter-counters[i].before_counter;
  136. sprintf(value,"%d -> %d (%s%d)",
  137. counters[i].before_counter,
  138. counters[i].after_counter,
  139. (diff_counter>0?"+":""),
  140. diff_counter);
  141. }
  142. #endif
  143. void
  144. counters_as_entry(Slapi_Entry* e)
  145. {
  146. #if defined(DEBUG)
  147. int i;
  148. fetch_counters();
  149. for(i=0;i<num_counters;i++)
  150. {
  151. char value[40];
  152. char *type= (char*)malloc(counter_size(&counters[i])+4);
  153. counter_dump(type,value,i);
  154. slapi_entry_attr_set_charptr( e, type, value);
  155. free(type);
  156. }
  157. #endif
  158. }
  159. void
  160. counters_to_errors_log(const char *text)
  161. {
  162. #if defined(DEBUG)
  163. int i;
  164. fetch_counters();
  165. LDAPDebug( LDAP_DEBUG_ANY, "Counter Dump - %s\n",text, 0, 0);
  166. for(i=0;i<num_counters;i++)
  167. {
  168. char value[40];
  169. char *type= (char*)malloc(counter_size(&counters[i])+4);
  170. counter_dump(type,value,i);
  171. LDAPDebug( LDAP_DEBUG_ANY, "%s %s\n",type, value, 0);
  172. free(type);
  173. }
  174. #endif
  175. }