| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: chuck_stats.cpp |
| 28 | // desc: statistics - for audicle |
| 29 | // |
| 30 | // author: Ge Wang ([email protected]) |
| 31 | // Perry R. Cook ([email protected]) |
| 32 | // Philip Davidson ([email protected]) |
| 33 | // Ananya Misra ([email protected]) |
| 34 | // date: Autumn 2004 |
| 35 | //----------------------------------------------------------------------------- |
| 36 | #include "chuck_stats.h" |
| 37 | |
| 38 | // tracking |
| 39 | #if defined(__CHUCK_STAT_TRACK__) |
| 40 | |
| 41 | #include "chuck_vm.h" |
| 42 | using namespace std; |
| 43 | |
| 44 | // static members |
| 45 | Chuck_Stats * Chuck_Stats::our_instance = NULL; |
| 46 | t_CKBOOL Chuck_Stats::activations_yes = FALSE; |
| 47 | |
| 48 | |
| 49 | //----------------------------------------------------------------------------- |
| 50 | // name: instance() |
| 51 | // desc: ... |
| 52 | //----------------------------------------------------------------------------- |
| 53 | Chuck_Stats * Chuck_Stats::instance() |
| 54 | { |
| 55 | if( !our_instance ) |
| 56 | { |
| 57 | our_instance = new Chuck_Stats; |
| 58 | assert( our_instance ); |
| 59 | } |
| 60 | |
| 61 | return our_instance; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | //----------------------------------------------------------------------------- |
| 68 | // name: add_shred() |
| 69 | // desc: ... |
| 70 | //----------------------------------------------------------------------------- |
| 71 | void Chuck_Stats::add_shred( Chuck_VM_Shred * shred ) |
| 72 | { |
| 73 | assert( vm != NULL ); |
| 74 | assert( shred->stat == NULL ); |
| 75 | assert( shred->xid != 0 ); |
| 76 | |
| 77 | Shred_Stat * stat = new Shred_Stat; |
| 78 | stat->xid = shred->xid; |
| 79 | stat->parent = shred->parent ? shred->parent->xid : 0; |
| 80 | stat->state = 0; // inactive |
| 81 | stat->shred_ref = shred; |
| 82 | stat->spork_time = shred->wake_time; |
| 83 | stat->wake_time = shred->wake_time; |
| 84 | stat->name = shred->name; |
| 85 | stat->owner = "noone"; |
| 86 | stat->source = "remote"; |
| 87 | if( strncmp( shred->name.c_str(), "editor->", 8 ) == 0 ) |
| 88 | { |
| 89 | stat->name = shred->name.c_str() + 8; |
| 90 | stat->source = "editor"; |
| 91 | } |
| 92 | if( shred->parent ) |
| 93 | { |
| 94 | Shred_Stat * another = shreds[shred->parent->xid]; |
| 95 | another->mutex.acquire(); |
| 96 | another->children.push_back( stat ); |
| 97 | another->mutex.release(); |
| 98 | } |
| 99 | |
| 100 | // attach to shred |
| 101 | shred->stat = stat; |
| 102 | // add |
| 103 | mutex.acquire(); |
| 104 | shreds[shred->xid] = stat; |
| 105 | mutex.release(); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | //----------------------------------------------------------------------------- |
| 112 | // name: activate_shred() |
| 113 | // desc: ... |
| 114 | //----------------------------------------------------------------------------- |
| 115 | void Chuck_Stats::activate_shred( Chuck_VM_Shred * shred ) |
| 116 | { |
| 117 | assert( vm != NULL ); |
| 118 | //assert( shred->stat != NULL ); |
| 119 | //assert( shred->id != 0 ); |
| 120 | |
| 121 | Shred_Stat * stat = shred->stat; |
| 122 | // set active state |
| 123 | stat->state = 1; // active |
| 124 | // increment activations |
| 125 | stat->activations++; |
| 126 | // set the active time |
| 127 | stat->active_time = shred->wake_time; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | //----------------------------------------------------------------------------- |
| 134 | // name: advance_time() |
| 135 | // desc: ... |
| 136 | //----------------------------------------------------------------------------- |
| 137 | void Chuck_Stats::advance_time( Chuck_VM_Shred * shred, t_CKTIME to ) |
| 138 | { |
| 139 | assert( vm != NULL ); |
| 140 | |
| 141 | Shred_Stat * stat = shred->stat; |
| 142 | // set the wake_time |
| 143 | stat->wake_time = to; |
| 144 | // find the period |
| 145 | t_CKDUR diff = stat->wake_time - stat->active_time; |
| 146 | // add to diffs |
| 147 | stat->diff_total += diff; |
| 148 | // put in queue |
| 149 | stat->diffs.push( diff ); |
| 150 | // subtract |
| 151 | if( stat->diffs.size() > stat->num_diffs ) |
| 152 | { |
| 153 | stat->diff_total -= stat->diffs.front(); |
| 154 | stat->diffs.pop(); |
| 155 | } |
| 156 | // calculate average |
| 157 | stat->average_ctrl = stat->diff_total / stat->diffs.size(); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | //----------------------------------------------------------------------------- |
| 164 | // name: deactivate_shred() |
| 165 | // desc: ... |
| 166 | //----------------------------------------------------------------------------- |
| 167 | void Chuck_Stats::deactivate_shred( Chuck_VM_Shred * shred ) |
| 168 | { |
| 169 | assert( vm != NULL ); |
| 170 | |
| 171 | Shred_Stat * stat = shred->stat; |
| 172 | // get diff |
| 173 | t_CKUINT diff = stat->cycles - stat->last_cycles; |
| 174 | // set active state |
| 175 | stat->state = 2; |
| 176 | // find average |
| 177 | stat->act_cycles.push( diff ); |
| 178 | // add to total |
| 179 | stat->act_cycles_total += diff; |
| 180 | // subtract |
| 181 | if( stat->act_cycles.size() > stat->num_diffs ) |
| 182 | { |
| 183 | stat->act_cycles_total -= stat->act_cycles.front(); |
| 184 | stat->act_cycles.pop(); |
| 185 | } |
| 186 | // the average |
| 187 | stat->average_cycles = (t_CKFLOAT)stat->act_cycles_total / stat->act_cycles.size(); |
| 188 | // remember the cycle count |
| 189 | stat->last_cycles = stat->cycles; |
| 190 | if( activations_yes ) |
| 191 | { |
| 192 | stat->mutex.acquire(); |
| 193 | stat->activationss.push_back( Shred_Activation( vm->shreduler()->now_system, diff ) ); |
| 194 | stat->mutex.release(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | //----------------------------------------------------------------------------- |
| 202 | // name: remove_shred() |
| 203 | // desc: ... |
| 204 | //----------------------------------------------------------------------------- |
| 205 | void Chuck_Stats::remove_shred( Chuck_VM_Shred * shred ) |
| 206 | { |
| 207 | assert( vm != NULL ); |
| 208 | |
| 209 | Shred_Stat * stat = shred->stat; |
| 210 | // set state |
| 211 | stat->state = 3; // deleted |
| 212 | // set free time |
| 213 | stat->free_time = vm->shreduler()->now_system; |
| 214 | // remove |
| 215 | mutex.acquire(); |
| 216 | shreds.erase( shreds.find( stat->xid ) ); |
| 217 | done.push_back( stat ); |
| 218 | mutex.release(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | //----------------------------------------------------------------------------- |
| 225 | // name: get_shreds() |
| 226 | // desc: ... |
| 227 | //----------------------------------------------------------------------------- |
| 228 | void Chuck_Stats::get_shreds( vector<Shred_Stat *> & out, map<Shred_Stat *, Shred_Stat *> & d ) |
| 229 | { |
| 230 | assert( vm != NULL ); |
| 231 | |
| 232 | map<t_CKUINT, Shred_Stat *>::iterator iter; |
| 233 | out.clear(); |
| 234 | |
| 235 | mutex.acquire(); |
| 236 | for( iter = shreds.begin(); iter != shreds.end(); iter++ ) |
| 237 | out.push_back( (*iter).second ); |
| 238 | for( int i = 0; i < done.size(); i++ ) |
| 239 | d[done[i]] = done[i]; |
| 240 | done.clear(); |
| 241 | mutex.release(); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | //----------------------------------------------------------------------------- |
| 249 | // name: Chuck_Stats() |
| 250 | // desc: ... |
| 251 | //----------------------------------------------------------------------------- |
| 252 | Chuck_Stats::Chuck_Stats() |
| 253 | { |
| 254 | vm = NULL; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | //----------------------------------------------------------------------------- |
| 261 | // name: ~Chuck_Stats() |
| 262 | // desc: ... |
| 263 | //----------------------------------------------------------------------------- |
| 264 | Chuck_Stats::~Chuck_Stats() |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | //----------------------------------------------------------------------------- |
| 272 | // name: get_sporked() |
| 273 | // desc: ... |
| 274 | //----------------------------------------------------------------------------- |
| 275 | void Shred_Stat::get_sporked( vector<Shred_Stat *> & out ) |
| 276 | { |
| 277 | out.clear(); |
| 278 | |
| 279 | mutex.acquire(); |
| 280 | for( int i = 0; i < children.size(); i++ ) |
| 281 | out.push_back( children[i] ); |
| 282 | mutex.release(); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | //----------------------------------------------------------------------------- |
| 289 | // name: get_activations() |
| 290 | // desc: ... |
| 291 | //----------------------------------------------------------------------------- |
| 292 | void Shred_Stat::get_activations( vector<Shred_Activation> & out ) |
| 293 | { |
| 294 | out.clear(); |
| 295 | |
| 296 | mutex.acquire(); |
| 297 | out = activationss; |
| 298 | activationss.clear(); |
| 299 | mutex.release(); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | #endif |