cnstream_inspect.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <rapidjson/document.h>
  21. #include <rapidjson/rapidjson.h>
  22. #include <rapidjson/stringbuffer.h>
  23. #include <rapidjson/writer.h>
  24. #include <dirent.h>
  25. #include <dlfcn.h>
  26. #include <errno.h>
  27. #include <getopt.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <algorithm>
  31. #include <fstream>
  32. #include <iomanip>
  33. #include <string>
  34. #include <vector>
  35. #include "cnstream_logging.hpp"
  36. #include "cnstream_module.hpp"
  37. #include "cnstream_pipeline.hpp"
  38. #include "cnstream_version.hpp"
  39. static void Usage() {
  40. std::cout << "Usage:" << std::endl;
  41. std::cout << "\t inspect-tool [OPTION...] [MODULE-NAME]" << std::endl;
  42. std::cout << "Options: " << std::endl;
  43. std::cout << std::left << std::setw(40) << "\t -h, --help"
  44. << "Show usage" << std::endl;
  45. std::cout << std::left << std::setw(40) << "\t -a, --all"
  46. << "Print all modules" << std::endl;
  47. std::cout << std::left << std::setw(40) << "\t -m, --module-name"
  48. << "List the module parameters" << std::endl;
  49. std::cout << std::left << std::setw(40) << "\t -v, --version"
  50. << "Print version information\n"
  51. << std::endl;
  52. }
  53. static const struct option long_option[] = {{"help", no_argument, nullptr, 'h'},
  54. {"all", no_argument, nullptr, 'a'},
  55. {"module-name", required_argument, nullptr, 'm'},
  56. {"version", no_argument, nullptr, 'v'},
  57. {nullptr, 0, nullptr, 0}};
  58. static void PrintVersion() {
  59. std::cout << "CNStream: " << cnstream::VersionString() << std::endl;
  60. return;
  61. }
  62. static uint32_t GetFirstLetterPos(std::string desc, uint32_t begin, uint32_t length) {
  63. if (begin + length > desc.length()) {
  64. length = desc.length() - begin;
  65. }
  66. for (uint32_t i = 0; i < length; i++) {
  67. if (desc.substr(begin + i, 1) != " ") {
  68. return begin + i;
  69. }
  70. }
  71. return begin;
  72. }
  73. static uint32_t GetLastSpacePos(std::string desc, uint32_t end, uint32_t length) {
  74. if (end > desc.length()) {
  75. end = desc.length();
  76. }
  77. if (end < length) {
  78. length = end;
  79. }
  80. for (uint32_t i = 0; i < length; i++) {
  81. if (desc.substr(end - i, 1) == " ") {
  82. return end - i;
  83. }
  84. }
  85. return end;
  86. }
  87. static uint32_t GetSubStrEnd(std::string desc, uint32_t begin, uint32_t sub_str_len) {
  88. if (begin + sub_str_len < desc.length()) {
  89. return GetLastSpacePos(desc, begin + sub_str_len, sub_str_len);
  90. } else {
  91. return desc.length();
  92. }
  93. }
  94. static void PrintDesc(std::string desc, uint32_t indent, uint32_t sub_len) {
  95. uint32_t len = desc.length();
  96. uint32_t sub_begin = GetFirstLetterPos(desc, 0, sub_len);
  97. uint32_t sub_end = GetSubStrEnd(desc, sub_begin, sub_len);
  98. // std::cout << std::left << std::setw(first_width) << desc.substr(sub_begin, sub_end - sub_begin) << std::endl;
  99. std::cout << desc.substr(sub_begin, sub_end - sub_begin) << std::endl;
  100. while (sub_begin + sub_len < len) {
  101. sub_begin = GetFirstLetterPos(desc, sub_end, sub_len);
  102. sub_end = GetSubStrEnd(desc, sub_begin, sub_len);
  103. std::cout << std::left << std::setw(indent) << "" << desc.substr(sub_begin, sub_end - sub_begin) << std::endl;
  104. if (sub_end != len && sub_end + sub_len >= len) {
  105. sub_begin = GetFirstLetterPos(desc, sub_end, len - sub_end);
  106. std::cout << std::left << std::setw(indent) << "" << desc.substr(sub_begin, len - sub_begin) << std::endl;
  107. }
  108. }
  109. }
  110. static void PrintAllModulesDesc() {
  111. const uint32_t width = 40;
  112. const uint32_t sub_str_len = 80;
  113. std::vector<std::string> modules = cnstream::ModuleFactory::Instance()->GetRegisted();
  114. cnstream::ModuleCreatorWorker creator;
  115. std::cout << "\033[01;32m"<< std::left << std::setw(width) << "Module Name"
  116. << "Description" << "\033[01;0m" << std::endl;
  117. for (auto& it : modules) {
  118. cnstream::Module* module = creator.Create(it, it);
  119. std::cout << "\033[01;1m" << std::left << std::setw(width) << it << "\033[0m";
  120. std::string desc = module->param_register_.GetModuleDesc();
  121. PrintDesc(desc, width, sub_str_len);
  122. std::cout << std::endl;
  123. delete module;
  124. }
  125. }
  126. static void PrintModuleCommonParameters() {
  127. const uint32_t width = 30;
  128. const uint32_t sub_str_len = 80;
  129. std::cout << "\033[01;32m" << " " << std::left << std::setw(width) << "Common Parameter"
  130. << "Description" << "\033[0m" << std::endl;
  131. std::cout << "\033[01;1m" << " " << std::left << std::setw(width) << "class_name" << "\033[0m";
  132. PrintDesc("Module class name.", width + 2, sub_str_len);
  133. std::cout << std::endl;
  134. std::cout << "\033[01;1m" << " " << std::left << std::setw(width) << "parallelism" << "\033[0m";
  135. PrintDesc("Module parallelism.", width + 2, sub_str_len);
  136. std::cout << std::endl;
  137. std::cout << "\033[01;1m" << " " << std::left << std::setw(width) << "max_input_queue_size" << "\033[0m";
  138. PrintDesc("Max size of module input queue.", width + 2, sub_str_len);
  139. std::cout << std::endl;
  140. std::cout << "\033[01;1m" << " " << std::left << std::setw(width) << "next_modules" << "\033[0m";
  141. PrintDesc("Next modules.", width + 2, sub_str_len);
  142. std::cout << std::endl;
  143. }
  144. static void PrintModuleParameters(const std::string& module_name) {
  145. const uint32_t width = 30;
  146. const uint32_t sub_str_len = 80;
  147. std::string name = module_name;
  148. cnstream::ModuleCreatorWorker creator;
  149. cnstream::Module* module = creator.Create(name, name);
  150. if (nullptr == module) {
  151. name = "cnstream::" + name;
  152. module = creator.Create(name, name.substr(10));
  153. if (nullptr == module) {
  154. std::cout << "No such module: '" << module_name << "'." << std::endl;
  155. return;
  156. }
  157. }
  158. auto module_params = module->param_register_.GetParams();
  159. std::cout <<"\033[01;33m" << module_name << " Details:" << "\033[0m" << std::endl;
  160. PrintModuleCommonParameters();
  161. std::cout << "\033[01;32m" << " " << std::left << std::setw(width) << "Custom Parameter"
  162. << "Description" << "\033[0m" << std::endl;
  163. for (auto& it : module_params) {
  164. std::cout << "\033[01;1m" << " " << std::left << std::setw(width) << it.first << "\033[0m";
  165. PrintDesc(it.second, width + 2, sub_str_len);
  166. std::cout << std::endl;
  167. }
  168. delete module;
  169. }
  170. int main(int argc, char* argv[]) {
  171. int opt = 0;
  172. bool getopt = false;
  173. std::string config_file;
  174. std::string module_name;
  175. std::stringstream ss;
  176. if (argc == 1) {
  177. PrintAllModulesDesc();
  178. return 0;
  179. }
  180. while ((opt = getopt_long(argc, argv, "ham:c:v", long_option, nullptr)) != -1) {
  181. getopt = true;
  182. switch (opt) {
  183. case 'h':
  184. Usage();
  185. break;
  186. case 'a':
  187. PrintAllModulesDesc();
  188. break;
  189. case 'm':
  190. ss.clear();
  191. ss.str("");
  192. ss << optarg;
  193. module_name = ss.str();
  194. PrintModuleParameters(module_name);
  195. break;
  196. case 'v':
  197. PrintVersion();
  198. break;
  199. default:
  200. return 0;
  201. }
  202. }
  203. if (!getopt) {
  204. for (int i = 1; i < argc; i++) {
  205. ss.clear();
  206. ss.str("");
  207. ss << argv[i];
  208. module_name = ss.str();
  209. PrintModuleParameters(module_name);
  210. std::cout << std::endl;
  211. }
  212. }
  213. return 0;
  214. }