Jack2  1.9.8
JackTools.h
1 /*
2  Copyright (C) 2006-2008 Grame
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as published by
6  the Free Software Foundation; either version 2.1 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 #ifndef __JackTools__
21 #define __JackTools__
22 
23 #ifdef WIN32
24 #include <windows.h>
25 #define DIR_SEPARATOR '\\'
26 #else
27 #define DIR_SEPARATOR '/'
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #endif
33 
34 #ifdef __APPLE__
35 #include <sys/syslimits.h>
36 #endif
37 
38 #include "jslist.h"
39 #include "driver_interface.h"
40 #include "JackCompilerDeps.h"
41 #include "JackError.h"
42 #include "JackException.h"
43 
44 #include <string>
45 #include <algorithm>
46 #include <vector>
47 #include <iostream>
48 #include <fstream>
49 
50 namespace Jack
51 {
52 
57  struct SERVER_EXPORT JackTools
58  {
59  static int GetPID();
60  static int GetUID();
61 
62  static void KillServer();
63 
64  static int MkDir(const char* path);
65  static char* UserDir();
66  static char* ServerDir(const char* server_name, char* server_dir);
67  static const char* DefaultServerName();
68  static void CleanupFiles(const char* server_name);
69  static int GetTmpdir();
70  static void RewriteName(const char* name, char* new_name);
71  static void ThrowJackNetException();
72 
73  // For OSX only
74  static int ComputationMicroSec(int buffer_size)
75  {
76  if (buffer_size < 128) {
77  return 500;
78  } else if (buffer_size < 256) {
79  return 300;
80  } else {
81  return 100;
82  }
83  }
84  };
85 
103  template <class T> class JackGnuPlotMonitor
104  {
105  private:
106  uint32_t fMeasureCnt;
107  uint32_t fMeasurePoints;
108  uint32_t fMeasureId;
109  T* fCurrentMeasure;
110  T** fMeasureTable;
111  uint32_t fTablePos;
112  std::string fName;
113 
114  public:
115  JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
116  {
117  jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
118 
119  fMeasureCnt = measure_cnt;
120  fMeasurePoints = measure_points;
121  fTablePos = 0;
122  fName = name;
123  fCurrentMeasure = new T[fMeasurePoints];
124  fMeasureTable = new T*[fMeasureCnt];
125  for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
126  {
127  fMeasureTable[cnt] = new T[fMeasurePoints];
128  fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
129  }
130  }
131 
133  {
134  jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
135 
136  for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
137  delete[] fMeasureTable[cnt];
138  delete[] fMeasureTable;
139  delete[] fCurrentMeasure;
140  }
141 
142  T AddNew ( T measure_point )
143  {
144  fMeasureId = 0;
145  return fCurrentMeasure[fMeasureId++] = measure_point;
146  }
147 
148  uint32_t New()
149  {
150  return fMeasureId = 0;
151  }
152 
153  T Add ( T measure_point )
154  {
155  return fCurrentMeasure[fMeasureId++] = measure_point;
156  }
157 
158  uint32_t AddLast ( T measure_point )
159  {
160  fCurrentMeasure[fMeasureId] = measure_point;
161  fMeasureId = 0;
162  return Write();
163  }
164 
165  uint32_t Write()
166  {
167  for ( uint32_t point = 0; point < fMeasurePoints; point++ )
168  fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
169  if ( ++fTablePos == fMeasureCnt )
170  fTablePos = 0;
171  return fTablePos;
172  }
173 
174  int Save ( std::string name = std::string ( "" ) )
175  {
176  std::string filename = ( name.empty() ) ? fName : name;
177  filename += ".log";
178 
179  jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
180 
181  std::ofstream file ( filename.c_str() );
182 
183  for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
184  {
185  for ( uint32_t point = 0; point < fMeasurePoints; point++ )
186  file << fMeasureTable[cnt][point] << " \t";
187  file << std::endl;
188  }
189 
190  file.close();
191  return 0;
192  }
193 
194  int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
195  std::string* field_names = NULL, uint32_t field_number = 0,
196  std::string name = std::string ( "" ) )
197  {
198  std::string title = ( name.empty() ) ? fName : name;
199  std::string plot_filename = title + ".plt";
200  std::string data_filename = title + ".log";
201 
202  std::ofstream file ( plot_filename.c_str() );
203 
204  file << "set multiplot" << std::endl;
205  file << "set grid" << std::endl;
206  file << "set title \"" << title << "\"" << std::endl;
207 
208  for ( uint32_t i = 0; i < options_number; i++ )
209  file << options_list[i] << std::endl;
210 
211  file << "plot ";
212  for ( uint32_t row = 1; row <= field_number; row++ )
213  {
214  file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
215  file << ( ( row < field_number ) ? ", " : "\n" );
216  }
217 
218  jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
219 
220  file.close();
221  return 0;
222  }
223  };
224 
225  void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
226  void PrintLoadError(const char* so_name);
227 
228 }
229 
230 #endif