Jack2 1.9.12

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 "JackCompilerDeps.h"
40 #include "JackError.h"
41
42 #include <string>
43 #include <algorithm>
44 #include <vector>
45 #include <iostream>
46 #include <fstream>
47
48 namespace Jack
49 {
50
55 struct SERVER_EXPORT JackTools
56 {
57 static int GetPID();
58 static int GetUID();
59
60 static void KillServer();
61
62 static int MkDir(const char* path);
63 static char* UserDir();
64 static char* ServerDir(const char* server_name, char* server_dir);
65 static const char* DefaultServerName();
66 static void CleanupFiles(const char* server_name);
67 static int GetTmpdir();
68 static void RewriteName(const char* name, char* new_name);
69 static void ThrowJackNetException();
70
71 // For OSX only
72 static int ComputationMicroSec(int buffer_size)
73 {
74 if (buffer_size < 128) {
75 return 500;
76 } else if (buffer_size < 256) {
77 return 300;
78 } else {
79 return 100;
80 }
81 }
82 };
83
101 template <class T> class JackGnuPlotMonitor
102 {
103 private:
104 uint32_t fMeasureCnt;
105 uint32_t fMeasurePoints;
106 uint32_t fMeasureId;
107 T* fCurrentMeasure;
108 T** fMeasureTable;
109 uint32_t fTablePos;
110 std::string fName;
111
112 public:
113 JackGnuPlotMonitor(uint32_t measure_cnt, uint32_t measure_points, std::string name)
114 {
115 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
116
117 fMeasureCnt = measure_cnt;
118 fMeasurePoints = measure_points;
119 fTablePos = 0;
120 fName = name;
121 fCurrentMeasure = new T[fMeasurePoints];
122 fMeasureTable = new T*[fMeasureCnt];
123 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
124 {
125 fMeasureTable[cnt] = new T[fMeasurePoints];
126 std::fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
127 }
128 }
129
130 ~JackGnuPlotMonitor()
131 {
132 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
133
134 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
135 delete[] fMeasureTable[cnt];
136 delete[] fMeasureTable;
137 delete[] fCurrentMeasure;
138 }
139
140 T AddNew(T measure_point)
141 {
142 fMeasureId = 0;
143 return fCurrentMeasure[fMeasureId++] = measure_point;
144 }
145
146 uint32_t New()
147 {
148 return fMeasureId = 0;
149 }
150
151 T Add(T measure_point)
152 {
153 return fCurrentMeasure[fMeasureId++] = measure_point;
154 }
155
156 uint32_t AddLast(T measure_point)
157 {
158 fCurrentMeasure[fMeasureId] = measure_point;
159 fMeasureId = 0;
160 return Write();
161 }
162
163 uint32_t Write()
164 {
165 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
166 fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
167 if ( ++fTablePos == fMeasureCnt )
168 fTablePos = 0;
169 return fTablePos;
170 }
171
172 int Save(std::string name = std::string ( "" ))
173 {
174 std::string filename = ( name.empty() ) ? fName : name;
175 filename += ".log";
176
177 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
178
179 std::ofstream file ( filename.c_str() );
180
181 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
182 {
183 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
184 file << fMeasureTable[cnt][point] << " \t";
185 file << std::endl;
186 }
187
188 file.close();
189 return 0;
190 }
191
192 int SetPlotFile(std::string* options_list, uint32_t options_number,
193 std::string* field_names, uint32_t field_number,
194 std::string name = std::string ( "" ))
195 {
196 std::string title = ( name.empty() ) ? fName : name;
197 std::string plot_filename = title + ".plt";
198 std::string data_filename = title + ".log";
199
200 std::ofstream file ( plot_filename.c_str() );
201
202 file << "set multiplot" << std::endl;
203 file << "set grid" << std::endl;
204 file << "set title \"" << title << "\"" << std::endl;
205
206 for ( uint32_t i = 0; i < options_number; i++ )
207 file << options_list[i] << std::endl;
208
209 file << "plot ";
210 for ( uint32_t row = 1; row <= field_number; row++ )
211 {
212 file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
213 file << ( ( row < field_number ) ? ", " : "\n" );
214 }
215
216 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
217
218 file.close();
219 return 0;
220 }
221
222 };
223
224 void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
225 void PrintLoadError(const char* so_name);
226
227 }
228
229 #endif
Jack::JackTools
Utility functions.
Definition: JackTools.h:55
Jack::JackGnuPlotMonitor
Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
Definition: JackTools.h:101
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108