1#!/bin/bash
2# Copyright 2014 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6CPUPATH=/sys/devices/system/cpu
7
8MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}')
9
10set_governor() {
11  echo "Setting CPU frequency governor to \"$1\""
12  for (( i=0; i<=$MAXID; i++ )); do
13    echo "$1" > $CPUPATH/cpu$i/cpufreq/scaling_governor
14  done
15}
16
17enable_cores() {
18  # $1: How many cores to enable.
19  for (( i=1; i<=$MAXID; i++ )); do
20    if [ "$i" -lt "$1" ]; then
21      echo 1 > $CPUPATH/cpu$i/online
22    else
23      echo 0 > $CPUPATH/cpu$i/online
24    fi
25  done
26}
27
28dual_core() {
29  echo "Switching to dual-core mode"
30  enable_cores 2
31}
32
33single_core() {
34  echo "Switching to single-core mode"
35  enable_cores 1
36}
37
38
39all_cores() {
40  echo "Reactivating all CPU cores"
41  enable_cores $((MAXID+1))
42}
43
44
45limit_cores() {
46  # $1: How many cores to enable.
47  echo "Limiting to $1 cores"
48  enable_cores $1
49}
50
51case "$1" in
52  fast | performance)
53    set_governor "performance"
54    ;;
55  slow | powersave)
56    set_governor "powersave"
57    ;;
58  default | ondemand)
59    set_governor "ondemand"
60    ;;
61  dualcore | dual)
62    dual_core
63    ;;
64  singlecore | single)
65    single_core
66    ;;
67  allcores | all)
68    all_cores
69    ;;
70  limit_cores)
71    if [ $# -ne 2 ]; then
72      echo "Usage $0 limit_cores <num>"
73      exit 1
74    fi
75    limit_cores $2
76    ;;
77  *)
78    echo "Usage: $0 fast|slow|default|singlecore|dualcore|all|limit_cores"
79    exit 1
80    ;;
81esac
82