Fix merge conflicts.
[jack2.git] / common / JackTools.h
blob5960fed88d7cf0e53e6603f10fc18c9771cea037
1 /*
2 Copyright (C) 2006-2008 Grame
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.
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.
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.
20 #ifndef __JackTools__
21 #define __JackTools__
23 #ifdef WIN32
24 #include <windows.h>
25 #else
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #endif
31 #ifdef __APPLE__
32 #include <sys/syslimits.h>
33 #endif
35 #include "jslist.h"
36 #include "driver_interface.h"
37 #include "JackCompilerDeps.h"
38 #include "JackError.h"
39 #include "JackException.h"
41 #include <string>
42 #include <algorithm>
43 #include <vector>
44 #include <iostream>
45 #include <fstream>
47 namespace Jack
50 /*!
51 \brief Utility functions.
54 struct SERVER_EXPORT JackTools
56 static int GetPID();
57 static int GetUID();
59 static void KillServer();
61 static char* UserDir();
62 static char* ServerDir ( const char* server_name, char* server_dir );
63 static const char* DefaultServerName();
64 static void CleanupFiles ( const char* server_name );
65 static int GetTmpdir();
66 static void RewriteName ( const char* name, char* new_name );
68 static void ThrowJackNetException();
71 /*!
72 \brief Generic monitoring class. Saves data to GnuPlot files ('.plt' and '.log' datafile)
74 This template class allows to manipulate monitoring records, and automatically generate the GnuPlot config and data files.
75 Operations are RT safe because it uses fixed size data buffers.
76 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
84 You can also call SetPlotFile() to automatically generate '.plt' file from an options list.
88 template <class T> class JackGnuPlotMonitor
90 private:
91 uint32_t fMeasureCnt;
92 uint32_t fMeasurePoints;
93 uint32_t fMeasureId;
94 T* fCurrentMeasure;
95 T** fMeasureTable;
96 uint32_t fTablePos;
97 std::string fName;
99 public:
100 JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
102 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
104 fMeasureCnt = measure_cnt;
105 fMeasurePoints = measure_points;
106 fTablePos = 0;
107 fName = name;
108 fCurrentMeasure = new T[fMeasurePoints];
109 fMeasureTable = new T*[fMeasureCnt];
110 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
112 fMeasureTable[cnt] = new T[fMeasurePoints];
113 fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
117 ~JackGnuPlotMonitor()
119 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
121 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
122 delete[] fMeasureTable[cnt];
123 delete[] fMeasureTable;
124 delete[] fCurrentMeasure;
127 T AddNew ( T measure_point )
129 fMeasureId = 0;
130 return fCurrentMeasure[fMeasureId++] = measure_point;
133 uint32_t New()
135 return fMeasureId = 0;
138 T Add ( T measure_point )
140 return fCurrentMeasure[fMeasureId++] = measure_point;
143 uint32_t AddLast ( T measure_point )
145 fCurrentMeasure[fMeasureId] = measure_point;
146 fMeasureId = 0;
147 return Write();
150 uint32_t Write()
152 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
153 fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
154 if ( ++fTablePos == fMeasureCnt )
155 fTablePos = 0;
156 return fTablePos;
159 int Save ( std::string name = std::string ( "" ) )
161 std::string filename = ( name.empty() ) ? fName : name;
162 filename += ".log";
164 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
166 std::ofstream file ( filename.c_str() );
168 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
170 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
171 file << fMeasureTable[cnt][point] << " \t";
172 file << std::endl;
175 file.close();
176 return 0;
179 int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
180 std::string* field_names = NULL, uint32_t field_number = 0,
181 std::string name = std::string ( "" ) )
183 std::string title = ( name.empty() ) ? fName : name;
184 std::string plot_filename = title + ".plt";
185 std::string data_filename = title + ".log";
187 std::ofstream file ( plot_filename.c_str() );
189 file << "set multiplot" << std::endl;
190 file << "set grid" << std::endl;
191 file << "set title \"" << title << "\"" << std::endl;
193 for ( uint32_t i = 0; i < options_number; i++ )
194 file << options_list[i] << std::endl;
196 file << "plot ";
197 for ( uint32_t row = 1; row <= field_number; row++ )
199 file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
200 file << ( ( row < field_number ) ? ", " : "\n" );
203 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
205 file.close();
206 return 0;
210 void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
211 void PrintLoadError(const char* so_name);
215 #endif