timer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. \file timer.h
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief timer Implementation
  17. */
  18. #ifndef __TIMER_H__
  19. #define __TIMER_H__
  20. #include <cstdint>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. class CSocket;
  25. //Note do not change P_FIRST and P_LAST and keep them pointing to the first and last element in the enum
  26. enum ABYPHASE {
  27. P_TOTAL, P_INIT, P_CIRCUIT, P_NETWORK, P_BASE_OT, P_SETUP, P_OT_EXT, P_GARBLE, P_ONLINE, P_FIRST = P_TOTAL, P_LAST = P_ONLINE
  28. };
  29. // Structure for measuring runtime
  30. struct aby_timings {
  31. double timing;
  32. timespec tbegin;
  33. timespec tend;
  34. };
  35. // Structure for counting communication
  36. struct aby_comm {
  37. uint64_t totalcomm;
  38. uint64_t cbegin;
  39. uint64_t cend;
  40. };
  41. extern aby_timings m_tTimes[P_LAST - P_FIRST + 1];
  42. extern aby_comm m_tSend[P_LAST - P_FIRST + 1];
  43. extern aby_comm m_tRecv[P_LAST - P_FIRST + 1];
  44. /**
  45. * Return time difference in milliseconds
  46. */
  47. double getMillies(timespec timestart, timespec timeend);
  48. /**
  49. * Start measuring runtime for a given phase
  50. * @param msg - a message for debugging
  51. * @param phase - the ABY phase to measure
  52. */
  53. void StartWatch(const std::string& msg, ABYPHASE phase);
  54. /**
  55. * Stop measuring runtime
  56. * Called after StartWatch() with identical phase parameter
  57. * @param msg - a message for debugging
  58. * @param phase - the ABY phase to measure
  59. */
  60. void StopWatch(const std::string& msg, ABYPHASE phase);
  61. /**
  62. * Start measuring both runtime and communication
  63. * @param msg - a message for debugging
  64. * @param phase - the ABY phase to measure
  65. * @param sock - a vector of sockets
  66. */
  67. void StartRecording(const std::string& msg, ABYPHASE phase,
  68. const std::vector<std::unique_ptr<CSocket>>& sock);
  69. /**
  70. * Stop measuring both runtime and communication
  71. * Called after StartRecording() with identical phase parameter
  72. * @param msg - a message for debugging
  73. * @param phase - the ABY phase to measure
  74. * @param sock - a vector of sockets
  75. */
  76. void StopRecording(const std::string& msg, ABYPHASE phase,
  77. const std::vector<std::unique_ptr<CSocket>>& sock);
  78. void PrintTimings();
  79. void PrintCommunication();
  80. inline double GetTimeForPhase(ABYPHASE phase) {
  81. return m_tTimes[phase].timing;
  82. }
  83. inline uint64_t GetSentDataForPhase(ABYPHASE phase) {
  84. return m_tSend[phase].totalcomm;
  85. }
  86. inline uint64_t GetReceivedDataForPhase(ABYPHASE phase) {
  87. return m_tRecv[phase].totalcomm;
  88. }
  89. #endif /* TIMER_H_ */