sgx_profile.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifdef _PROFILE_
  32. #include <iostream>
  33. #include <vector>
  34. #include <fstream>
  35. #include "sgx_profile.h"
  36. #include "se_time.h"
  37. #include <string.h>
  38. using namespace std;
  39. typedef struct _profile_item_t{
  40. const char *str; /* tag */
  41. int flag; /* 0: start, 1: end */
  42. long long time; /* current time */
  43. } profile_item_t;
  44. static vector<profile_item_t> profile_items;
  45. static int alloc_size;
  46. static int used_size;
  47. const int MALLOC_SIZE = 1000;
  48. static long long freq = {0};
  49. #define MALLOC_TAG "PROFILE_MALLOC_CONSUMED_TIME"
  50. extern "C" void profile_init()
  51. {
  52. freq = se_get_tick_count_freq();
  53. profile_items.resize(MALLOC_SIZE);
  54. alloc_size = MALLOC_SIZE;
  55. used_size = 0;
  56. }
  57. static void profile_add_info(const char *str, int flag)
  58. {
  59. long long cur_time = se_get_tick_count();
  60. if(used_size==alloc_size){
  61. alloc_size+=MALLOC_SIZE;
  62. profile_items.resize(alloc_size);
  63. profile_items[used_size].flag = PRO_START;
  64. profile_items[used_size].str = MALLOC_TAG;
  65. profile_items[used_size].time = cur_time;
  66. cur_time = se_get_tick_count();
  67. used_size++;
  68. profile_items[used_size].flag = PRO_END;
  69. profile_items[used_size].str = MALLOC_TAG;
  70. profile_items[used_size].time = cur_time;
  71. used_size++;
  72. }
  73. profile_items[used_size].flag = flag;
  74. profile_items[used_size].str = str;
  75. profile_items[used_size].time = cur_time;
  76. used_size++;
  77. }
  78. extern "C" void profile_start(const char* str)
  79. {
  80. profile_add_info(str, PRO_START);
  81. }
  82. extern "C" void profile_end(const char * str)
  83. {
  84. profile_add_info(str, PRO_END);
  85. }
  86. #include <string>
  87. std::string get_prof_fun_name(const char *s)
  88. {
  89. std::string input(s);
  90. size_t end = input.find("(");
  91. size_t begin = input.substr(0,end).rfind(" ")+1;
  92. end = end - begin;
  93. return input.substr(begin,end);
  94. }
  95. extern "C" void profile_output(const char* filename)
  96. {
  97. int i,j;
  98. ofstream fs;
  99. fs.open(filename); /* do not overwritten previous value */
  100. fs << "freq: " << freq <<endl;
  101. fs << "tag" << "," << "start_cycle" << "," << "end_cycle" << endl;
  102. for(i=0; i<used_size; i ++)
  103. {
  104. if(profile_items[i].flag!=PRO_START)
  105. continue;
  106. for(j = i + 1 ; j <used_size; j++)
  107. {
  108. if(strcmp(profile_items[i].str, profile_items[j].str) == 0)
  109. {
  110. if(profile_items[j].flag == PRO_END)
  111. break;
  112. else
  113. {
  114. /* cout << "Error: find another start for " << it->str << endl; */
  115. return;
  116. }
  117. }
  118. }
  119. if(j == used_size)
  120. {
  121. /* cout << "Error: not find end for " << it->str << endl; */
  122. return;
  123. }
  124. fs << get_prof_fun_name(profile_items[i].str) << "," << profile_items[i].time << "," << profile_items[j].time << endl;
  125. }
  126. profile_items.clear();
  127. used_size=0;
  128. alloc_size=0;
  129. fs.close();
  130. }
  131. #endif