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.

230 lines
6.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. #define DIR_SEPARATOR '\\'
  20. #else
  21. #define DIR_SEPARATOR '/'
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #include <dirent.h>
  26. #endif
  27. #ifdef __APPLE__
  28. #include <sys/syslimits.h>
  29. #endif
  30. #include "jslist.h"
  31. #include "JackCompilerDeps.h"
  32. #include "JackError.h"
  33. #include <string>
  34. #include <algorithm>
  35. #include <vector>
  36. #include <iostream>
  37. #include <fstream>
  38. namespace Jack
  39. {
  40. /*!
  41. \brief Utility functions.
  42. */
  43. struct SERVER_EXPORT JackTools
  44. {
  45. static int GetPID();
  46. static int GetUID();
  47. static void KillServer();
  48. static int MkDir(const char* path);
  49. static const char* UserDir();
  50. static const char* ServerDir(const char* server_name, char* server_dir);
  51. static const char* DefaultServerName();
  52. static void CleanupFiles(const char* server_name);
  53. static int GetTmpdir();
  54. static void RewriteName(const char* name, char* new_name);
  55. static void ThrowJackNetException();
  56. // For OSX only
  57. static int ComputationMicroSec(int buffer_size)
  58. {
  59. if (buffer_size < 128) {
  60. return 500;
  61. } else if (buffer_size < 256) {
  62. return 300;
  63. } else {
  64. return 100;
  65. }
  66. }
  67. };
  68. /*!
  69. \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
  70. This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
  71. Operations are RT safe because it uses fixed size data buffers.
  72. You can set the number of measure points, and the number of records.
  73. To use it :
  74. - create a JackGnuPlotMonitor, you can use the data type you want.
  75. - create a temporary array for your measure
  76. - once you have filled this array with 'measure points' value, call write() to add it to the record
  77. - once you've done with your measurement, just call save() to save your data file
  78. You can also call SetPlotFile() to automatically generate '.plt' file from an options list.
  79. */
  80. template <class T> class JackGnuPlotMonitor
  81. {
  82. private:
  83. uint32_t fMeasureCnt;
  84. uint32_t fMeasurePoints;
  85. uint32_t fMeasureId;
  86. T* fCurrentMeasure;
  87. T** fMeasureTable;
  88. uint32_t fTablePos;
  89. std::string fName;
  90. public:
  91. JackGnuPlotMonitor(uint32_t measure_cnt, uint32_t measure_points, std::string name)
  92. {
  93. jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
  94. fMeasureCnt = measure_cnt;
  95. fMeasurePoints = measure_points;
  96. fTablePos = 0;
  97. fName = name;
  98. fCurrentMeasure = new T[fMeasurePoints];
  99. fMeasureTable = new T*[fMeasureCnt];
  100. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  101. {
  102. fMeasureTable[cnt] = new T[fMeasurePoints];
  103. std::fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
  104. }
  105. }
  106. ~JackGnuPlotMonitor()
  107. {
  108. jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
  109. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  110. delete[] fMeasureTable[cnt];
  111. delete[] fMeasureTable;
  112. delete[] fCurrentMeasure;
  113. }
  114. T AddNew(T measure_point)
  115. {
  116. fMeasureId = 0;
  117. return fCurrentMeasure[fMeasureId++] = measure_point;
  118. }
  119. uint32_t New()
  120. {
  121. return fMeasureId = 0;
  122. }
  123. T Add(T measure_point)
  124. {
  125. return fCurrentMeasure[fMeasureId++] = measure_point;
  126. }
  127. uint32_t AddLast(T measure_point)
  128. {
  129. fCurrentMeasure[fMeasureId] = measure_point;
  130. fMeasureId = 0;
  131. return Write();
  132. }
  133. uint32_t Write()
  134. {
  135. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  136. fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
  137. if ( ++fTablePos == fMeasureCnt )
  138. fTablePos = 0;
  139. return fTablePos;
  140. }
  141. int Save(std::string name = std::string ( "" ))
  142. {
  143. std::string filename = ( name.empty() ) ? fName : name;
  144. filename += ".log";
  145. jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
  146. std::ofstream file ( filename.c_str() );
  147. for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
  148. {
  149. for ( uint32_t point = 0; point < fMeasurePoints; point++ )
  150. file << fMeasureTable[cnt][point] << " \t";
  151. file << std::endl;
  152. }
  153. file.close();
  154. return 0;
  155. }
  156. int SetPlotFile(std::string* options_list, uint32_t options_number,
  157. std::string* field_names, uint32_t field_number,
  158. std::string name = std::string ( "" ))
  159. {
  160. std::string title = ( name.empty() ) ? fName : name;
  161. std::string plot_filename = title + ".plt";
  162. std::string data_filename = title + ".log";
  163. std::ofstream file ( plot_filename.c_str() );
  164. file << "set multiplot" << std::endl;
  165. file << "set grid" << std::endl;
  166. file << "set title \"" << title << "\"" << std::endl;
  167. for ( uint32_t i = 0; i < options_number; i++ )
  168. file << options_list[i] << std::endl;
  169. file << "plot ";
  170. for ( uint32_t row = 1; row <= field_number; row++ )
  171. {
  172. file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
  173. file << ( ( row < field_number ) ? ", " : "\n" );
  174. }
  175. jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
  176. file.close();
  177. return 0;
  178. }
  179. };
  180. void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
  181. void PrintLoadError(const char* so_name);
  182. }
  183. #endif