counters.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <prcountr.h>
  39. #include "slap.h"
  40. #if defined(DEBUG)
  41. struct counter
  42. {
  43. const char *qname;
  44. const char *rname;
  45. const char *description;
  46. PRUint32 before_counter;
  47. PRUint32 after_counter;
  48. };
  49. static int num_counters= 0;
  50. static struct counter *counters= NULL;
  51. static int
  52. count_counters()
  53. {
  54. int i= 0;
  55. PR_DEFINE_COUNTER(qh);
  56. PR_INIT_COUNTER_HANDLE(qh,NULL);
  57. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  58. while(qh!=NULL)
  59. {
  60. PR_DEFINE_COUNTER(rh);
  61. PR_INIT_COUNTER_HANDLE(rh,NULL);
  62. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  63. while(rh!=NULL)
  64. {
  65. i++;
  66. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  67. }
  68. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  69. }
  70. return i;
  71. }
  72. static int
  73. do_fetch_counters()
  74. {
  75. int i= 0;
  76. PR_DEFINE_COUNTER(qh);
  77. PR_INIT_COUNTER_HANDLE(qh,NULL);
  78. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  79. while(qh!=NULL)
  80. {
  81. PR_DEFINE_COUNTER(rh);
  82. PR_INIT_COUNTER_HANDLE(rh,NULL);
  83. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  84. while(rh!=NULL)
  85. {
  86. if(i<num_counters)
  87. {
  88. counters[i].before_counter= counters[i].after_counter;
  89. PR_GET_COUNTER_NAME_FROM_HANDLE(rh,&counters[i].qname,&counters[i].rname,&counters[i].description);
  90. PR_GET_COUNTER(counters[i].after_counter,rh);
  91. }
  92. i++;
  93. PR_FIND_NEXT_COUNTER_RNAME(rh,rh,qh);
  94. }
  95. PR_FIND_NEXT_COUNTER_QNAME(qh,qh);
  96. }
  97. return i;
  98. }
  99. static void
  100. fetch_counters()
  101. {
  102. int i;
  103. if(counters==NULL)
  104. {
  105. num_counters= count_counters();
  106. counters= (struct counter*)calloc(num_counters,sizeof(struct counter));
  107. }
  108. i= do_fetch_counters();
  109. if(i>num_counters)
  110. {
  111. free(counters);
  112. counters= NULL;
  113. num_counters= i;
  114. counters= (struct counter*)calloc(num_counters,sizeof(struct counter));
  115. do_fetch_counters();
  116. }
  117. }
  118. static size_t
  119. counter_size(struct counter *counter)
  120. {
  121. size_t r= 0;
  122. r+= (counter->qname?strlen(counter->qname):0);
  123. r+= (counter->rname?strlen(counter->rname):0);
  124. r+= (counter->description?strlen(counter->description):0);
  125. return r;
  126. }
  127. static void
  128. counter_dump(char *name, char *value, int i)
  129. {
  130. PRUint32 diff_counter;
  131. sprintf(name,"%s_%s_%s",counters[i].qname,counters[i].rname,counters[i].description);
  132. diff_counter= counters[i].after_counter-counters[i].before_counter;
  133. sprintf(value,"%d -> %d (%s%d)",
  134. counters[i].before_counter,
  135. counters[i].after_counter,
  136. (diff_counter>0?"+":""),
  137. diff_counter);
  138. }
  139. #endif
  140. void
  141. counters_as_entry(Slapi_Entry* e)
  142. {
  143. #if defined(DEBUG)
  144. int i;
  145. fetch_counters();
  146. for(i=0;i<num_counters;i++)
  147. {
  148. char value[40];
  149. char *type= (char*)malloc(counter_size(&counters[i])+4);
  150. counter_dump(type,value,i);
  151. slapi_entry_attr_set_charptr( e, type, value);
  152. free(type);
  153. }
  154. #endif
  155. }
  156. void
  157. counters_to_errors_log(const char *text)
  158. {
  159. #if defined(DEBUG)
  160. int i;
  161. fetch_counters();
  162. LDAPDebug( LDAP_DEBUG_ANY, "Counter Dump - %s\n",text, 0, 0);
  163. for(i=0;i<num_counters;i++)
  164. {
  165. char value[40];
  166. char *type= (char*)malloc(counter_size(&counters[i])+4);
  167. counter_dump(type,value,i);
  168. LDAPDebug( LDAP_DEBUG_ANY, "%s %s\n",type, value, 0);
  169. free(type);
  170. }
  171. #endif
  172. }