util.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include "util.hpp"
  21. #if defined(_WIN32) || defined(_WIN64)
  22. #include <io.h>
  23. #elif defined(__linux) || defined(__unix)
  24. #include <dirent.h>
  25. #include <unistd.h>
  26. #endif
  27. #include <gflags/gflags.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <string.h>
  31. #include <cerrno>
  32. #include <fstream>
  33. #include <limits>
  34. #include <list>
  35. #include <string>
  36. #include <vector>
  37. #include "cnstream_logging.hpp"
  38. #include "profiler/module_profiler.hpp"
  39. DEFINE_int32(perf_level, 0, "perf level");
  40. std::string GetExePath() {
  41. char path[PATH_MAX_LENGTH];
  42. int cnt = readlink("/proc/self/exe", path, PATH_MAX_LENGTH);
  43. if (cnt < 0 || cnt >= PATH_MAX_LENGTH) {
  44. return NULL;
  45. }
  46. for (int i = cnt - 1; i >= 0; --i) {
  47. if ('/' == path[i]) {
  48. path[i + 1] = '\0';
  49. break;
  50. }
  51. }
  52. std::string result(path);
  53. return result;
  54. }
  55. void CheckExePath(const std::string &path) {
  56. extern int errno;
  57. if (path.size() == 0) {
  58. LOGF_IF(DEMO, 0 != errno) << std::string(strerror(errno));
  59. LOGF(DEMO) << "length of exe path is larger than " << PATH_MAX_LENGTH;
  60. }
  61. }
  62. inline bool exists_file(const std::string &name) { return (access(name.c_str(), F_OK) != -1); }
  63. std::list<std::string> ReadFileList(const std::string &list) {
  64. std::ifstream ifile;
  65. ifile.open(list);
  66. std::list<std::string> files;
  67. if (ifile) {
  68. std::string path;
  69. while (std::getline(ifile, path)) {
  70. std::string file = path;
  71. if (file != "") {
  72. files.push_back(file);
  73. path.clear();
  74. }
  75. }
  76. } else {
  77. LOGE(DEMO) << "Open file: " << list.c_str() << " failed.";
  78. exit(0);
  79. }
  80. ifile.close();
  81. return files;
  82. }
  83. std::vector<std::string> LoadLabels(const std::string &filename) {
  84. std::vector<std::string> labels;
  85. std::ifstream file(filename);
  86. LOGF_IF(DEMO, !file.is_open()) << "file:" << filename << " open failed.";
  87. std::string line;
  88. while (std::getline(file, line)) {
  89. labels.push_back(std::string(line));
  90. }
  91. file.close();
  92. return labels;
  93. }
  94. bool CheckDir(const std::string &path, std::string *estr) {
  95. bool ret = true;
  96. struct stat st;
  97. if (::stat(path.c_str(), &st)) {
  98. // errmsg
  99. ret = false;
  100. *estr = std::string("Check dir '") + path + "' failed: " + std::string(strerror(errno));
  101. } else {
  102. if (!(st.st_mode & S_IFDIR)) {
  103. *estr = std::string("Check dir '") + path + "' failed: " + std::string("Not a directory");
  104. ret = false;
  105. } else {
  106. if (access(path.c_str(), W_OK)) {
  107. ret = false;
  108. *estr = std::string("Check dir '") + path + "' failed: " + std::string(strerror(errno));
  109. }
  110. }
  111. }
  112. return ret;
  113. }
  114. std::list<std::string> GetFileNameFromDir(const std::string &dir, const char *filter) {
  115. std::list<std::string> files;
  116. #if defined(_WIN32) || defined(_WIN64)
  117. int64_t hFile = 0;
  118. struct _finddata_t fileinfo;
  119. std::string path;
  120. if ((hFile = _findfirst(path.assign(dir).append("/" + std::string(filter)).c_str(), &fileinfo)) != -1) {
  121. do {
  122. if (!(fileinfo.attrib & _A_SUBDIR)) { // not directory
  123. std::string file_path = dir + "/" + fileinfo.name;
  124. files.push_back(file_path);
  125. }
  126. } while (_findnext(hFile, &fileinfo) == 0);
  127. _findclose(hFile);
  128. }
  129. #elif defined(__linux) || defined(__unix)
  130. DIR *pDir = nullptr;
  131. struct dirent *pEntry;
  132. pDir = opendir(dir.c_str());
  133. if (pDir != nullptr) {
  134. while ((pEntry = readdir(pDir)) != nullptr) {
  135. if (strcmp(pEntry->d_name, ".") == 0 || strcmp(pEntry->d_name, "..") == 0
  136. || strstr(pEntry->d_name, strstr(filter, "*") + 1) == nullptr || pEntry->d_type != DT_REG) { // regular file
  137. continue;
  138. }
  139. std::string file_path = dir + "/" + pEntry->d_name;
  140. files.push_back(file_path);
  141. }
  142. closedir(pDir);
  143. }
  144. #endif
  145. return files;
  146. }
  147. size_t GetFileSize(const std::string &filename) {
  148. #if defined(_WIN32) || defined(_WIN64)
  149. struct _stat file_stat;
  150. _stat(filename.c_str(), &file_stat);
  151. return file_stat.st_size;
  152. #elif defined(__linux) || defined(__unix)
  153. struct stat file_stat;
  154. stat(filename.c_str(), &file_stat);
  155. return file_stat.st_size;
  156. #endif
  157. }
  158. static
  159. std::string FindTheSlowestOne(const cnstream::PipelineProfile& profile) {
  160. std::string slowest_module_name = "";
  161. double minimum_fps = std::numeric_limits<double>::max();
  162. for (const auto& module_profile : profile.module_profiles) {
  163. for (const auto& process_profile : module_profile.process_profiles) {
  164. if (process_profile.process_name == cnstream::kPROCESS_PROFILER_NAME) {
  165. if (minimum_fps > process_profile.fps) {
  166. minimum_fps = process_profile.fps;
  167. slowest_module_name = module_profile.module_name;
  168. }
  169. }
  170. }
  171. }
  172. return slowest_module_name;
  173. }
  174. static
  175. std::string FillStr(std::string str, uint32_t length, char charactor) {
  176. int filled_length = (length - str.length()) / 2;
  177. filled_length = filled_length > 0 ? filled_length : 0;
  178. int remainder = 0;
  179. if (filled_length && (length - str.length()) % 2) remainder = 1;
  180. return std::string(filled_length + remainder, charactor) + str + std::string(filled_length, charactor);
  181. }
  182. static
  183. void PrintProcessPerformance(std::ostream& os, const cnstream::ProcessProfile& profile) {
  184. if (FLAGS_perf_level <= 1) {
  185. if (FLAGS_perf_level == 1) {
  186. os << "[Latency]: (Avg): " << profile.latency << "ms";
  187. os << ", (Min): " << profile.minimum_latency << "ms";
  188. os << ", (Max): " << profile.maximum_latency << "ms" << std::endl;
  189. }
  190. os << "[Counter]: " << profile.counter;
  191. os << ", [Throughput]: " << profile.fps << "fps" << std::endl;
  192. } else if (FLAGS_perf_level >=2) {
  193. os << "[Counter]: " << profile.counter;
  194. os << ", [Completed]: " << profile.completed;
  195. os << ", [Dropped]: " << profile.dropped;
  196. os << ", [Ongoing]: " << profile.ongoing << std::endl;
  197. os << "[Latency]: (Avg): " << profile.latency << "ms";
  198. os << ", (Min): " << profile.minimum_latency << "ms";
  199. os << ", (Max): " << profile.maximum_latency << "ms" << std::endl;
  200. os << "[Throughput]: " << profile.fps << "fps" << std::endl;
  201. }
  202. if (FLAGS_perf_level >= 3) {
  203. uint32_t stream_name_max_length = 15;
  204. if (profile.stream_profiles.size()) {
  205. os << "\n------ Stream ------\n";
  206. }
  207. for (const auto& stream_profile : profile.stream_profiles) {
  208. std::string stream_name = "[" + stream_profile.stream_name + "]";
  209. os << stream_name << std::string(stream_name_max_length - stream_name.length(), ' ');
  210. os << "[Counter]: " << stream_profile.counter;
  211. os << ", [Completed]: " << stream_profile.completed;
  212. os << ", [Dropped]: " << stream_profile.dropped << std::endl;
  213. os << std::string(stream_name_max_length, ' ');
  214. os << "[Latency]: (Avg): " << stream_profile.latency << "ms";
  215. os << ", (Min): " << stream_profile.minimum_latency << "ms";
  216. os << ", (Max): " << stream_profile.maximum_latency << "ms" << std::endl;
  217. os << std::string(stream_name_max_length, ' ');
  218. os << "[Throughput]: " << stream_profile.fps << "fps" << std::endl;
  219. }
  220. }
  221. }
  222. void PrintPipelinePerformance(const std::string& prefix_str, const cnstream::PipelineProfile& profile) {
  223. auto slowest_module_name = FindTheSlowestOne(profile);
  224. std::stringstream ss;
  225. int length = 80;
  226. ss << "\033[1m\033[36m" << FillStr(" Performance Print Start (" + prefix_str + ") ", length, '*') << "\033[0m\n";
  227. ss << "\033[1m" << FillStr(" Pipeline: [" + profile.pipeline_name + "] ", length, '=') << "\033[0m\n";
  228. for (const auto& module_profile : profile.module_profiles) {
  229. ss << "\033[1m\033[32m" << FillStr(" Module: [" + module_profile.module_name + "] ", length, '-');
  230. if (slowest_module_name == module_profile.module_name) {
  231. ss << "\033[0m\033[41m" << " (slowest) ";
  232. }
  233. ss << "\033[0m\n";
  234. for (const auto& process_profile : module_profile.process_profiles) {
  235. ss << "\033[1m\033[33m" << std::string(length / 8, '-');
  236. ss << "Process Name: [" << process_profile.process_name << "\033[0m" << "]\n";
  237. PrintProcessPerformance(ss, process_profile);
  238. }
  239. }
  240. ss << "\n\033[1m\033[32m" << FillStr(" Overall ", length, '-') << "\033[0m\n";
  241. PrintProcessPerformance(ss, profile.overall_profile);
  242. ss << "\033[1m\033[36m" << FillStr(" Performance Print End (" + prefix_str + ") ", length, '*') << "\033[0m\n";
  243. std::cout << ss.str() << std::endl;
  244. }