cmMakefileProfilingData.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMakefileProfilingData.h"
  4. #include <chrono>
  5. #include <stdexcept>
  6. #include <vector>
  7. #include <cm3p/json/value.h>
  8. #include <cm3p/json/writer.h>
  9. #include "cmsys/FStream.hxx"
  10. #include "cmsys/SystemInformation.hxx"
  11. #include "cmListFileCache.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. cmMakefileProfilingData::cmMakefileProfilingData(
  15. const std::string& profileStream)
  16. {
  17. std::ios::openmode omode = std::ios::out | std::ios::trunc;
  18. this->ProfileStream.open(profileStream.c_str(), omode);
  19. Json::StreamWriterBuilder wbuilder;
  20. this->JsonWriter =
  21. std::unique_ptr<Json::StreamWriter>(wbuilder.newStreamWriter());
  22. if (!this->ProfileStream.good()) {
  23. throw std::runtime_error(std::string("Unable to open: ") + profileStream);
  24. }
  25. this->ProfileStream << "[";
  26. };
  27. cmMakefileProfilingData::~cmMakefileProfilingData() noexcept
  28. {
  29. if (this->ProfileStream.good()) {
  30. try {
  31. this->ProfileStream << "]";
  32. this->ProfileStream.close();
  33. } catch (...) {
  34. cmSystemTools::Error("Error writing profiling output!");
  35. }
  36. }
  37. }
  38. void cmMakefileProfilingData::StartEntry(const cmListFileFunction& lff,
  39. cmListFileContext const& lfc)
  40. {
  41. /* Do not try again if we previously failed to write to output. */
  42. if (!this->ProfileStream.good()) {
  43. return;
  44. }
  45. try {
  46. if (this->ProfileStream.tellp() > 1) {
  47. this->ProfileStream << ",";
  48. }
  49. cmsys::SystemInformation info;
  50. Json::Value v;
  51. v["ph"] = "B";
  52. v["name"] = lff.LowerCaseName();
  53. v["cat"] = "cmake";
  54. v["ts"] = Json::Value::UInt64(
  55. std::chrono::duration_cast<std::chrono::microseconds>(
  56. std::chrono::steady_clock::now().time_since_epoch())
  57. .count());
  58. v["pid"] = static_cast<int>(info.GetProcessId());
  59. v["tid"] = 0;
  60. Json::Value argsValue;
  61. if (!lff.Arguments().empty()) {
  62. std::string args;
  63. for (auto const& a : lff.Arguments()) {
  64. args += (args.empty() ? "" : " ") + a.Value;
  65. }
  66. argsValue["functionArgs"] = args;
  67. }
  68. argsValue["location"] = lfc.FilePath + ":" + std::to_string(lfc.Line);
  69. v["args"] = argsValue;
  70. this->JsonWriter->write(v, &this->ProfileStream);
  71. } catch (std::ios_base::failure& fail) {
  72. cmSystemTools::Error(
  73. cmStrCat("Failed to write to profiling output: ", fail.what()));
  74. } catch (...) {
  75. cmSystemTools::Error("Error writing profiling output!");
  76. }
  77. }
  78. void cmMakefileProfilingData::StopEntry()
  79. {
  80. /* Do not try again if we previously failed to write to output. */
  81. if (!this->ProfileStream.good()) {
  82. return;
  83. }
  84. try {
  85. this->ProfileStream << ",";
  86. cmsys::SystemInformation info;
  87. Json::Value v;
  88. v["ph"] = "E";
  89. v["ts"] = Json::Value::UInt64(
  90. std::chrono::duration_cast<std::chrono::microseconds>(
  91. std::chrono::steady_clock::now().time_since_epoch())
  92. .count());
  93. v["pid"] = static_cast<int>(info.GetProcessId());
  94. v["tid"] = 0;
  95. this->JsonWriter->write(v, &this->ProfileStream);
  96. } catch (std::ios_base::failure& fail) {
  97. cmSystemTools::Error(
  98. cmStrCat("Failed to write to profiling output:", fail.what()));
  99. } catch (...) {
  100. cmSystemTools::Error("Error writing profiling output!");
  101. }
  102. }