power_profile.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:6k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* -*- Mode:C++; c-basic-offset:3; tab-width:3; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
  15.  *    names of its contributors may be used to endorse or promote products 
  16.  *    derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  19.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
  20.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  21.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  22.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  23.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  25.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  26.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  27.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  28.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30. #include"power_profile.h"
  31. #include <scheduler.h>
  32. #include<unistd.h>
  33. #include <assert.h>
  34. #include<stdio.h>
  35. class PowerElementListItem {
  36.   
  37. public:  
  38. PowerElementListItem();
  39. PowerElementListItem(PowerElement el);
  40. ~PowerElementListItem();
  41. PowerElementListItem* next;
  42. PowerElementListItem* prev;
  43. PowerElement el;
  44. };
  45.  
  46. PowerElementListItem::PowerElementListItem()
  47. {
  48. next=NULL;
  49. prev=NULL;
  50. el.power=0;
  51. el.time=0;
  52. }
  53. PowerElementListItem::PowerElementListItem(PowerElement srcel)
  54. {
  55. next=NULL;
  56. prev=NULL;
  57. el = srcel; 
  58. }
  59.  
  60. PowerElementListItem::~PowerElementListItem()
  61. {
  62. if (next!=NULL) delete (next);
  63. }
  64. #ifndef COMPILING_TEST_PROGRAM
  65. static class PowerProfileClass : public TclClass {
  66. public:
  67. PowerProfileClass() : TclClass("PowerProfile") {}
  68. TclObject* create(int, const char*const*) {
  69. return (new PowerProfile());
  70. }
  71. } class_powerprofile;
  72. #endif
  73.   
  74. PowerProfile::PowerProfile() 
  75. {
  76. statusRecording = false;
  77. powerListHead = new PowerElementListItem;
  78. if (powerListHead == NULL)
  79. {
  80. printf("FATAL: PowerProfile::PowerProfile(): out of memoryn");
  81. fflush(stdout);
  82. exit(1);
  83. }
  84. powerListTail = powerListHead;
  85. numElements = 1;
  86. lastReadElIndex = 1;
  87. lastReadElPtr = powerListHead;
  88. accumulatedPower = 0;
  89. #ifndef COMPILING_TEST_PROGRAM
  90. bind("debug_", &debug_);
  91. #endif
  92. }
  93.   
  94. PowerProfile::~PowerProfile() 
  95. {
  96. delete powerListHead;
  97. }
  98. void PowerProfile::startNewRecording()
  99. {
  100. if (statusRecording==true) {
  101. printf("WARNING: PowerProfile::startNewRecording() called while recordingn");
  102. }
  103. /* Free whole list (except head element) */
  104. delete (powerListHead->next);
  105. powerListHead->next = NULL;
  106. powerListHead->prev = NULL;
  107. powerListTail = powerListHead;
  108. statusRecording = true;
  109. powerListHead->el.power = accumulatedPower;
  110. powerListHead->el.time = 0;
  111. numElements = 1;
  112. lastReadElIndex = 0;
  113. lastReadElPtr = powerListHead;
  114. }
  115. void PowerProfile::addElement(PowerElement el)
  116. {
  117. if(debug_ > 1) printf("t%f - PowerProfile.addElement - instantpower=%-2.20f el.power=%-2.20fn", Scheduler::instance().clock(),accumulatedPower, el.power);
  118. accumulatedPower += el.power;
  119. /* If very near to zero, round to zero 
  120.  to try to reduce error propagation */ 
  121. if (accumulatedPower <= POWERPROFILE_ACC_POWER_ROUND_THRESH && accumulatedPower >= -POWERPROFILE_ACC_POWER_ROUND_THRESH)
  122. {
  123. accumulatedPower = 0;
  124. }
  125. if (statusRecording)
  126. {
  127. double time;
  128. time = el.time - referenceTime;
  129. if(debug_) printf("t%f - PowerProfile.addElement - REC - instantpower=%-2.20f el.power=%-2.20fn", Scheduler::instance().clock(),accumulatedPower, el.power);
  130. assert(time >= powerListTail->el.time);
  131. if (time == powerListTail->el.time)
  132. {
  133. /* no need to create a new element */
  134. powerListTail->el.power += el.power;
  135. }
  136. else 
  137. {
  138. powerListTail->next = new PowerElementListItem(el);
  139. (powerListTail->next)->prev = powerListTail;
  140. powerListTail=powerListTail->next;
  141. powerListTail->el.time = time;
  142. powerListTail->el.power = accumulatedPower;
  143. numElements++;
  144. }
  145. }
  146. else /* not recording */
  147. {
  148. referenceTime = el.time;
  149. }
  150. }
  151. void PowerProfile::stopRecording()
  152. {
  153. if (statusRecording==true) {
  154. statusRecording = false;
  155. } else {
  156. printf("WARNING: PowerProfile::stopRecording() called but recording was already stoppedn");
  157. }
  158. }
  159. PowerElement PowerProfile::getElement(int index)
  160. {
  161. // printf("Asked for index %dn",index);
  162. if ( (index < 0) || (index > numElements-1))
  163. {
  164. printf("WARNING: PowerProfile::getElement() has been passed a wrong index %d (numElements:%d)n",index,numElements);
  165. PowerElement ret;
  166. ret.power = 0;
  167. ret.time = 0;
  168. return ret;
  169. }
  170. if (statusRecording) 
  171. {
  172. printf("WARNING: PowerProfile::getElement() should not be called while recording valuesn");
  173. }
  174. /* if everything is correct */
  175. while (index > lastReadElIndex)
  176. {
  177. lastReadElPtr = lastReadElPtr->next;
  178. lastReadElIndex++;
  179. //printf("New index =  %dn",lastReadElIndex);
  180. assert(lastReadElPtr != NULL);
  181. }
  182. while (index < lastReadElIndex)
  183. {
  184. lastReadElPtr = lastReadElPtr->prev;
  185. lastReadElIndex--;
  186. //printf("New index =  %dn",lastReadElIndex);
  187. assert(lastReadElPtr != NULL);
  188. }
  189. //  if (index < lastReadElIndex)
  190. //  {
  191. //  /* We must search from head of list */
  192. //  index = 0; 
  193. //  lastReadElPtr = powerListHead;
  194. //  lastReadElIndex = 0;
  195. //  }
  196. //lastReadElPtr = powerListHead;
  197. //lastReadElIndex = 0;
  198. // printf("lastReadElIndex =  %dn",lastReadElIndex);
  199. /* Now walk the list starting from lastReadEl */
  200. //  while (lastReadElIndex < index)
  201. //  {
  202. //  lastReadElIndex++;
  203. //  //printf("New index =  %dn",lastReadElIndex);
  204. //  lastReadElPtr = lastReadElPtr->next;
  205. //  }
  206. return (lastReadElPtr->el);
  207. }