statistics.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file statistics.c
  19. * @author Ioannis Koutras (joko@microlab.ntua.gr)
  20. * @date September 2012
  21. * @brief Statistics function implementation.
  22. */
  23. #include "statistics.h"
  24. #include "dmmlib/dmmlib.h"
  25. #include "dmmlib_trace.h"
  26. /** Updates a dmmstats_t variable
  27. *
  28. * @param dmm_stats Pointer to the dmmstats_t variable.
  29. * @param event The event type, could be MALLOC or FREE.
  30. * */
  31. #ifdef REQUEST_SIZE_INFO
  32. /**
  33. * @param mem_req Requested memory size info.
  34. */
  35. #endif /* REQUEST_SIZE_INFO */
  36. void update_stats
  37. ( dmmstats_t *dmm_stats
  38. , dmm_event_type event
  39. #ifdef REQUEST_SIZE_INFO
  40. , size_t mem_req
  41. #endif /* REQUEST_SIZE_INFO */
  42. )
  43. {
  44. switch(event) {
  45. case MALLOC :
  46. #ifdef REQUEST_SIZE_INFO
  47. dmm_stats->total_mem_requested += mem_req;
  48. #endif /* REQUEST_SIZE_INFO */
  49. dmm_stats->live_objects++;
  50. dmm_stats->num_malloc++;
  51. break;
  52. case FREE :
  53. #ifdef REQUEST_SIZE_INFO
  54. dmm_stats->total_mem_requested -= mem_req;
  55. #endif /* REQUEST_SIZE_INFO */
  56. dmm_stats->live_objects--;
  57. dmm_stats->num_free++;
  58. break;
  59. #ifdef WITH_REALLOC
  60. case REALLOC_LT :
  61. #ifdef REQUEST_SIZE_INFO
  62. dmm_stats->total_mem_requested -= mem_req;
  63. #endif /* REQUEST_SIZE_INFO */
  64. dmm_stats->num_realloc++;
  65. break;
  66. case REALLOC_GT :
  67. #ifdef REQUEST_SIZE_INFO
  68. dmm_stats->total_mem_requested += mem_req;
  69. #endif /* REQUEST_SIZE_INFO */
  70. dmm_stats->num_realloc++;
  71. break;
  72. #endif /* WITH_REALLOC */
  73. #ifdef WITH_MEMALIGN
  74. case MEMALIGN :
  75. #ifdef REQUEST_SIZE_INFO
  76. dmm_stats->total_mem_requested += mem_req;
  77. #endif /* REQUEST_SIZE_INFO */
  78. dmm_stats->live_objects++;
  79. dmm_stats->num_memalign++;
  80. break;
  81. #endif /* WITH_MEMALIGN */
  82. default :
  83. break;
  84. }
  85. STATS_TRACE("dmmlib - ms all %zu\n", dmm_stats->total_mem_allocated);
  86. #ifdef REQUEST_SIZE_INFO
  87. STATS_TRACE("dmmlib - ms req %zu\n", dmm_stats->total_mem_requested);
  88. #endif /* REQUEST_SIZE_INFO */
  89. }
  90. #ifdef REQUEST_SIZE_INFO
  91. /** Returns the total memory currently requested by the application. */
  92. size_t get_total_requested_memory(void) {
  93. return systemallocator.dmm_stats.total_mem_requested;
  94. }
  95. #endif /* REQUEST_SIZE_INFO */
  96. size_t get_total_allocated_memory(void) {
  97. return systemallocator.dmm_stats.total_mem_allocated;
  98. }
  99. long double get_utilization_index(void) {
  100. if(systemallocator.dmm_stats.total_mem_allocated != 0) {
  101. return (long double) systemallocator.dmm_stats.total_mem_requested /
  102. systemallocator.dmm_stats.total_mem_allocated;
  103. } else {
  104. return 1.0;
  105. }
  106. }