*** empty log message ***
[chuck-blob.git] / v2 / chuck_stats.cpp
blob97d76eec4127cbfdd0365d829b4314f455a1a8ac
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_stats.cpp
27 // desc: ...
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Philip Davidson (philipd@cs.princeton.edu)
32 // Ananya Misra (amisra@cs.princeton.edu)
33 // date: Autumn 2004
34 //-----------------------------------------------------------------------------
35 #include "chuck_stats.h"
37 // tracking
38 #if defined(__CHUCK_STAT_TRACK__)
40 #include "chuck_vm.h"
41 using namespace std;
43 // static members
44 Chuck_Stats * Chuck_Stats::our_instance = NULL;
45 t_CKBOOL Chuck_Stats::activations_yes = FALSE;
48 //-----------------------------------------------------------------------------
49 // name: instance()
50 // desc: ...
51 //-----------------------------------------------------------------------------
52 Chuck_Stats * Chuck_Stats::instance()
54 if( !our_instance )
56 our_instance = new Chuck_Stats;
57 assert( our_instance );
60 return our_instance;
66 //-----------------------------------------------------------------------------
67 // name: add_shred()
68 // desc: ...
69 //-----------------------------------------------------------------------------
70 void Chuck_Stats::add_shred( Chuck_VM_Shred * shred )
72 assert( vm != NULL );
73 assert( shred->stat == NULL );
74 assert( shred->xid != 0 );
76 Shred_Stat * stat = new Shred_Stat;
77 stat->xid = shred->xid;
78 stat->parent = shred->parent ? shred->parent->xid : 0;
79 stat->state = 0; // inactive
80 stat->shred_ref = shred;
81 stat->spork_time = shred->wake_time;
82 stat->wake_time = shred->wake_time;
83 stat->name = shred->name;
84 stat->owner = "noone";
85 stat->source = "remote";
86 if( strncmp( shred->name.c_str(), "editor->", 8 ) == 0 )
88 stat->name = shred->name.c_str() + 8;
89 stat->source = "editor";
91 if( shred->parent )
93 Shred_Stat * another = shreds[shred->parent->xid];
94 another->mutex.acquire();
95 another->children.push_back( stat );
96 another->mutex.release();
99 // attach to shred
100 shred->stat = stat;
101 // add
102 mutex.acquire();
103 shreds[shred->xid] = stat;
104 mutex.release();
110 //-----------------------------------------------------------------------------
111 // name: activate_shred()
112 // desc: ...
113 //-----------------------------------------------------------------------------
114 void Chuck_Stats::activate_shred( Chuck_VM_Shred * shred )
116 assert( vm != NULL );
117 //assert( shred->stat != NULL );
118 //assert( shred->id != 0 );
120 Shred_Stat * stat = shred->stat;
121 // set active state
122 stat->state = 1; // active
123 // increment activations
124 stat->activations++;
125 // set the active time
126 stat->active_time = shred->wake_time;
132 //-----------------------------------------------------------------------------
133 // name: advance_time()
134 // desc: ...
135 //-----------------------------------------------------------------------------
136 void Chuck_Stats::advance_time( Chuck_VM_Shred * shred, t_CKTIME to )
138 assert( vm != NULL );
140 Shred_Stat * stat = shred->stat;
141 // set the wake_time
142 stat->wake_time = to;
143 // find the period
144 t_CKDUR diff = stat->wake_time - stat->active_time;
145 // add to diffs
146 stat->diff_total += diff;
147 // put in queue
148 stat->diffs.push( diff );
149 // subtract
150 if( stat->diffs.size() > stat->num_diffs )
152 stat->diff_total -= stat->diffs.front();
153 stat->diffs.pop();
155 // calculate average
156 stat->average_ctrl = stat->diff_total / stat->diffs.size();
162 //-----------------------------------------------------------------------------
163 // name: deactivate_shred()
164 // desc: ...
165 //-----------------------------------------------------------------------------
166 void Chuck_Stats::deactivate_shred( Chuck_VM_Shred * shred )
168 assert( vm != NULL );
170 Shred_Stat * stat = shred->stat;
171 // get diff
172 t_CKUINT diff = stat->cycles - stat->last_cycles;
173 // set active state
174 stat->state = 2;
175 // find average
176 stat->act_cycles.push( diff );
177 // add to total
178 stat->act_cycles_total += diff;
179 // subtract
180 if( stat->act_cycles.size() > stat->num_diffs )
182 stat->act_cycles_total -= stat->act_cycles.front();
183 stat->act_cycles.pop();
185 // the average
186 stat->average_cycles = (t_CKFLOAT)stat->act_cycles_total / stat->act_cycles.size();
187 // remember the cycle count
188 stat->last_cycles = stat->cycles;
189 if( activations_yes )
191 stat->mutex.acquire();
192 stat->activationss.push_back( Shred_Activation( vm->shreduler()->now_system, diff ) );
193 stat->mutex.release();
200 //-----------------------------------------------------------------------------
201 // name: remove_shred()
202 // desc: ...
203 //-----------------------------------------------------------------------------
204 void Chuck_Stats::remove_shred( Chuck_VM_Shred * shred )
206 assert( vm != NULL );
208 Shred_Stat * stat = shred->stat;
209 // set state
210 stat->state = 3; // deleted
211 // set free time
212 stat->free_time = vm->shreduler()->now_system;
213 // remove
214 mutex.acquire();
215 shreds.erase( shreds.find( stat->xid ) );
216 done.push_back( stat );
217 mutex.release();
223 //-----------------------------------------------------------------------------
224 // name: get_shreds()
225 // desc: ...
226 //-----------------------------------------------------------------------------
227 void Chuck_Stats::get_shreds( vector<Shred_Stat *> & out, map<Shred_Stat *, Shred_Stat *> & d )
229 assert( vm != NULL );
231 map<t_CKUINT, Shred_Stat *>::iterator iter;
232 out.clear();
234 mutex.acquire();
235 for( iter = shreds.begin(); iter != shreds.end(); iter++ )
236 out.push_back( (*iter).second );
237 for( int i = 0; i < done.size(); i++ )
238 d[done[i]] = done[i];
239 done.clear();
240 mutex.release();
247 //-----------------------------------------------------------------------------
248 // name: Chuck_Stats()
249 // desc: ...
250 //-----------------------------------------------------------------------------
251 Chuck_Stats::Chuck_Stats()
253 vm = NULL;
259 //-----------------------------------------------------------------------------
260 // name: ~Chuck_Stats()
261 // desc: ...
262 //-----------------------------------------------------------------------------
263 Chuck_Stats::~Chuck_Stats()
270 //-----------------------------------------------------------------------------
271 // name: get_sporked()
272 // desc: ...
273 //-----------------------------------------------------------------------------
274 void Shred_Stat::get_sporked( vector<Shred_Stat *> & out )
276 out.clear();
278 mutex.acquire();
279 for( int i = 0; i < children.size(); i++ )
280 out.push_back( children[i] );
281 mutex.release();
287 //-----------------------------------------------------------------------------
288 // name: get_activations()
289 // desc: ...
290 //-----------------------------------------------------------------------------
291 void Shred_Stat::get_activations( vector<Shred_Activation> & out )
293 out.clear();
295 mutex.acquire();
296 out = activationss;
297 activationss.clear();
298 mutex.release();
302 #endif