# Per-Activity Power Usage

### From OLPC

## Contents |

# Background

As OLPC approaches its goal of highly effective power management, it will eventually be the case that different Activities cause the XO to draw different amounts of battery power. There has been some suggestion that users may wish to know how each Activity affects their battery life. By providing users with feedback about which Activities draw the most power, we may help them to extend their battery life, and also apply appropriate pressure on developers to increase the efficiency of their Activities. There might even be security benefits, in the case of activities running hidden spambots or password-crackers.

There are two main questions that a user might ask about each Activity: "how much power does it tend to use?", and "how much power is it using right now?". We may call these "historical" and "instantaneous" power usage.

The proposed power management systems derive most of their effectiveness from turning off the CPU when software does not need it. The system provides several important diagnostics for power management. The first is a Coulomb Counter, which allows the system software to determine how much charge has been drawn from the battery. The second is a wakeup history, which allows us to determine when the CPU was powered on or off.

# Instantaneous Power Usage

Instantaneous power usage is not well-defined on a per-activity basis due to the multitasking nature of the OS. It would not be difficult to determine the instantaneous *total* power usage by, for example, sampling the Coulomb Counter at one minute intervals and noting the rate of discharge.

If the system uses aggressive power management, then there may be several excellent proxies for instantaneous per-activity power usage. The best such proxy might be a combination of CPU utilization and system wakeups. The details of how to combine these two depend on the suspend system's idle detection strategies. For example, if idleness is detected solely on the basis of the time until the next scheduled event, then the best strategy might be to assign the entire power usage during each CPU-on period to the activity whose wakeup event started that period, or to the active activity if the CPU-on period was triggered by the user. If the idle detection is based on CPU usage, then one might instead instead divide power usage among activities in proportion to CPU usage. There are various steps in between these two options; the best solution will probably require empirical fiddling.

# Historical Power Usage

If instantaneous power usage numbers can be calculated with satisfactory accuracy, then the historical power usage can be calculated trivially, using simple techniques like exponential weighting. However, it seems likely that the instantaneous power usage statistics will not be especially accurate, due to the heuristic nature of the methods.

For historical power usage, we have a much more powerful tool: the Coulomb Counter. However, the Coulomb Counter only tells us aggregate system power usage. In order to determine the contribution of each activity to power usage, we must correlate the activity use history with the power usage history. This requires a model of how activities interact with regard to power usage.

## The Simplest Model: Additive power

The simplest model for power usage is that Activity *i* draws a wattage *w*_{i}. We let *w*_{0} represent the power used by the base system when no activities are running. If the set of running Activities at a given moment is *R*, then the total power usage .

Whenever the user starts or stops an activity under battery power, the system should make a log of the change in coulombs since the last reading, the time since the last reading, and the list of activities that were running since the last reading. Multiplying by the system voltage, each such record provides an equation for the number of Joules used: , where *t* is the time for that interval. To combine all of these measurements, we create a sparse matrix *A* with one column for each Activity and one row for each reading. An element *A*_{ij} is 0 if Activity *i* was not running during reading *j*. If Activity *i* *was* running during reading *j*, *A*_{ij} = *t*_{j}. We also create a column vector *b* with one entry for each reading, with *b*_{j} equal to the number of Joules used during reading *j*.

We may now formulate the wattage problem as *A**w* = *b*, where w is a column vector of wattages per activity that we wish to know. The matrix *A* is nonsquare, so this system cannot be solved by inversion. However, there are many well-known fast techniques for determining the least-squares solution to an over- or under-determined linear system, such as Conjugate Gradients on the Normal Equations (CGNR). Additionally, due to measurement error or modelling error, it is possible that the least-squares solution may produce negative wattages. To prevent this in an optimal fashion, we may restrict *w* to be all-positive and use fast Quadratic Programming methods to solve the restricted problem.

### Problems

This model is linear and additive; the presence of multiple activities does not affect how much power each one draws. This assumption is not very good, especially for high power usage activities. If two activities would each draw full power independently, running them simultaneously will not double the power usage. This model also does not account for level of user activity, network environment, or any other factors that may cause variation in power usage. Nonetheless, it is likely to be quite effective at distinguishing between the Activities that tend to draw a great deal of power and those that do not.

One possible improvement would be to allocate *w*_{1} to the backlight level, *w*_{2} to the DCON, etc. This will improve accuracy somewhat, but does not solve the fundamental problems with this model.

## A better model: additive usage with a ceiling

The laptop is only capable of drawing, at most, a maximum wattage *M*. Therefore, a better model of the system imagines that each activity would independently draw a wattage *w*_{i}, and the total system power drain is the sum of the *w*_{i} or the maximum wattage, whichever is lower. Mathematically, we would say . We may then follow the model from above, accounting for the power used during each period in which a fixed set of activities is open. If that power usage *J* is less than *M**t*, then we know that we are in the regime where , and we may add a row to the matrix *A*, as described above. Conversely, if we have *J* = *M**t*, then we know that . We may write this inequality as a constraint, in addition to the positive semi-definiteness requirement. Quadratic programming supports an unlimited number of such constraints, and the problem remains convex, and therefore quickly solvable.

### Problem: Measurement Error

These measurements of *J* and *M* will contain some error. As a result, it is entirely possible that *J* will never be measured as being quite equal to *M*. To relax this requirement somewhat, we may simply underestimate *M*. There is also the reverse problem: if a measurement *J* is slightly less than *M*, then it is possible that a measurement error will result in *J* crossing the threshold, and being added to the model as a constraint, rather than as a datapoint. In quadratic programming, constraints are absolute, so this erroneous constraint will not "average out".

To solve this problem, we may set a threshold *M*, and when it is crossed, impose an inequality constraint of the form , where ε is greater than any expected measurement error. This reduces the accuracy of the measurements for high-load activities, but also reduces the chance of spuriously constraining a set of activities whose actual power draw is much lower.

### Problem: Unlimited Growth

In this description *A* has a column for every Activity and a row for every measurement. If *A* is stored sparsely, and there are 5 activities running on average, then each measurement costs about 60 bytes in double precision. If the user starts or stops Activities 100 times a day, then *A* grows by 6 KB per day, or a little over 2 MB per year. This is too much storage, and the linear algebra on a matrix of this size becomes very expensive. One solution is to simply age out the oldest rows in *A* as new rows are added. However, to reach a reasonable size (e.g. 100 KB) entries would generally be forgotten in a month or less, which may be too short a time to accumulate enough data.

Luckily, there is a helpful trick that we may use. Many algorithms for minimizing | *A**x* − *b* | work by solving the equivalent equation *A*^{T}*A**x* = *A*^{T}*b*. The matrix *A*^{T}*A* is square if there are *N* Activities installed. If there are 100 installed Activities, then storing *A*^{T}*A* probably requires about 40 kB in double precision (the matrix is symmetrical, so we only have to store half the entries). Instead of appending a new row ρ to *A*, we can equivalently replace *A*^{T}*A* by *A*^{T}*A* + ρ^{T}ρ. Similarly, instead of appending a new entry *J* to the column vector *b* we may replace *A*^{T}*b* by *A*^{T}*b* + ρ^{T}*J*.

If *A*^{T}*A* grows too large, because the user has too many installed activities, then we may "age out" the least recently used Activities. This is somewhat harsher than aging out individual rows from *A*, but it is far less likely to be necessary, unless users typically have hundreds of Activities installed.

There remains the problem of unlimited growth in number of constraints. There is no equivalent way of amalgamating constraints, because there are 2^{N} possible constraints that might be generated. However, the constraints are also problematic because of the potential for one-time errors to result in erroneous hard constraints. In order to avoid permanent errors, and to conserve space, it seems sensible to age out the constraints. This is also advantageous because quadratic programming algorithms often run markedly slower as the number of constraints increases.

### Problem: Non-additivity of power

There is no limit to the complexity with which power usages of different programs may interact. Indeed, predicting the power usage of a program is necessarily as hard as the Halting Problem, and therefore uncomputable. The assumption that the total power usage is simply a sum of individual per-program power drains is clearly false. A more sophisticated approach might, for example, train a neural network to predict how much power any combination of running activities would be likely to draw.

However, our goal is to present the user with an indication of how much power each Activity draws. The fact that this number is not well-defined means that we must come up with a sensible definition for it. The above constrained-minimization seems as logical a definition as any.