sgx_profile.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /*
  32. Some notes for using the profiling macros
  33. 1. Define _PROFILE_ before including "sgx_profile.h" will enable the profiling,
  34. also need to include sgx_profile.cpp in the compiling process
  35. 2. When use it in multi-threaded application, please don't trigger profiling in multiple threads at the same time.
  36. The implementation is not thread-safe now, to avoid additional latency introduced by locks
  37. 3. PROFILE_OUTPUT macro should only be used once before application exits
  38. 4. PROFILE_START and PROFILE_END should be paired, otherwise PROFILE_OUTPUT will abort the program when detects the mismatch
  39. A simple example to use PROFILE macro
  40. #define _PROFILE_
  41. #include "sgx_profile.h"
  42. ...
  43. PROFILE_INIT();
  44. ...
  45. PROFILE_START("func1");
  46. func1();
  47. PROFILE_END("func1");
  48. ...
  49. PROFILE_START("func2");
  50. func2();
  51. PROFILE_END("func2");
  52. ...
  53. PROFILE_OUTPUT("C:\\work\\output.csv");
  54. ...
  55. */
  56. #ifndef _SGX_PROFILE_H_
  57. #define _SGX_PROFILE_H_
  58. #if defined(_PROFILE_)
  59. #define PRO_START 0
  60. #define PRO_END 1
  61. #if defined(__cplusplus)
  62. extern "C"
  63. {
  64. #endif
  65. void profile_init();
  66. void profile_start(const char* str); /* 'str' must be global string. Do not use string in stack. */
  67. void profile_end(const char * str);
  68. void profile_output(const char* filename);
  69. #if defined(__cplusplus)
  70. }
  71. #endif
  72. #define PROFILE_INIT() profile_init()
  73. #define PROFILE_START(x) profile_start(x)
  74. #define PROFILE_END(x) profile_end(x)
  75. #define PROFILE_OUTPUT(x) profile_output(x)
  76. #else
  77. #define PROFILE_INIT()
  78. #define PROFILE_START(x)
  79. #define PROFILE_END(x)
  80. #define PROFILE_OUTPUT(x)
  81. #endif
  82. #endif