jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.4KB

  1. /*
  2. Copyright (C) 2006-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackTools__
  16. #define __JackTools__
  17. #ifdef WIN32
  18. #include <windows.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <dirent.h>
  23. #endif
  24. #ifdef __APPLE__
  25. #include <sys/syslimits.h>
  26. #endif
  27. #include <string>
  28. #include <algorithm>
  29. #include <vector>
  30. #include <iostream>
  31. #include <fstream>
  32. #include "jslist.h"
  33. #include "driver_interface.h"
  34. #include "JackCompilerDeps.h"
  35. #include "JackError.h"
  36. namespace Jack
  37. {
  38. /*!
  39. \brief Utility functions.
  40. */
  41. struct SERVER_EXPORT JackTools
  42. {
  43. static int GetPID();
  44. static int GetUID();
  45. static char* UserDir();
  46. static char* ServerDir ( const char* server_name, char* server_dir );
  47. static const char* DefaultServerName();
  48. static void CleanupFiles ( const char* server_name );
  49. static int GetTmpdir();
  50. static void RewriteName ( const char* name, char* new_name );
  51. };
  52. /*!
  53. \brief Internal cient command line parser.
  54. */
  55. class SERVER_EXPORT JackArgParser
  56. {
  57. private:
  58. std::string fArgString;
  59. int fArgc;
  60. std::vector<std::string> fArgv;
  61. public:
  62. JackArgParser ( const char* arg );
  63. ~JackArgParser();
  64. std::string GetArgString();
  65. int GetNumArgv();
  66. int GetArgc();
  67. int GetArgv ( std::vector<std::string>& argv );
  68. int GetArgv ( char** argv );
  69. void DeleteArgv ( const char** argv );
  70. void ParseParams ( jack_driver_desc_t* desc, JSList** param_list );
  71. void FreeParams ( JSList* param_list );
  72. };
  73. /*!
  74. \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
  75. This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
  76. Operations are RT safe because it uses fixed size data buffers.
  77. You can set the number of measure points, and the number of records.
  78. To use it :
  79. - create a JackGnuPlotMonitor, you can use the data type you want.
  80. - create a temporary array for your measure
  81. - once you have filled this array with 'measure points' value, call write() to add it to the record
  82. - once you've done with your measurment, just call save() to save your data file
  83. You can also call SetPlotFile() to automatically generate '.plt' file from an options list.
  84. */
  85. template <class T> class JackGnuPlotMonitor
  86. {
  87. private:
  88. uint32_t fMeasureCnt;
  89. uint32_t fMeasurePoints;
  90. uint32_t fMeasureId;
  91. T* fCurrentMeasure;
  92. T** fMeasureTable;
  93. uint32_t fTablePos;
  94. std::string fName;
  95. public:
  96. JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
  97. {
  98. jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
  99. fMeasureCnt = measure_cnt;
  100. fMeasurePoints = measure_points;
  101. fTablePos = 0;
  102. fName = name;
  103. fCurrentMeasure = new T[fMeasurePoints];
  104. fMeasureTable = new T*[fMeasureCnt];
  105. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  106. {
  107. fMeasureTable[cnt] = new T[fMeasurePoints];
  108. fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
  109. }
  110. }
  111. ~JackGnuPlotMonitor()
  112. {
  113. jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
  114. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  115. delete[] fMeasureTable[cnt];
  116. delete[] fMeasureTable;
  117. delete[] fCurrentMeasure;
  118. }
  119. T AddNew ( T measure_point )
  120. {
  121. fMeasureId = 0;
  122. return fCurrentMeasure[fMeasureId++] = measure_point;
  123. }
  124. uint32_t New()
  125. {
  126. return fMeasureId = 0;
  127. }
  128. T Add ( T measure_point )
  129. {
  130. return fCurrentMeasure[fMeasureId++] = measure_point;
  131. }
  132. uint32_t AddLast ( T measure_point )
  133. {
  134. fCurrentMeasure[fMeasureId] = measure_point;
  135. fMeasureId = 0;
  136. return Write();
  137. }
  138. uint32_t Write()
  139. {
  140. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  141. fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
  142. if ( ++fTablePos == fMeasureCnt )
  143. fTablePos = 0;
  144. return fTablePos;
  145. }
  146. int Save ( std::string name = std::string ( "" ) )
  147. {
  148. std::string filename = ( name.empty() ) ? fName : name;
  149. filename += ".log";
  150. jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
  151. std::ofstream file ( filename.c_str() );
  152. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  153. {
  154. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  155. file << fMeasureTable[cnt][point] << " \t";
  156. file << std::endl;
  157. }
  158. file.close();
  159. return 0;
  160. }
  161. int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
  162. std::string* field_names = NULL, uint32_t field_number = 0,
  163. std::string name = std::string ( "" ) )
  164. {
  165. std::string title = ( name.empty() ) ? fName : name;
  166. std::string plot_filename = title + ".plt";
  167. std::string data_filename = title + ".log";
  168. std::ofstream file ( plot_filename.c_str() );
  169. file << "set multiplot" << std::endl;
  170. file << "set grid" << std::endl;
  171. file << "set title \"" << title << "\"" << std::endl;
  172. for ( uint32_t i = 0; i < options_number; i++ )
  173. file << options_list[i] << std::endl;
  174. file << "plot ";
  175. for ( uint32_t row = 1; row <= field_number; row++ )
  176. {
  177. file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
  178. file << ( ( row < field_number ) ? ", " : "\n" );
  179. }
  180. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
  181. file.close();
  182. return 0;
  183. }
  184. };
  185. }
  186. #endif