power: Add temp_logger init script

This script is current added to many boards in
chromeos-bsp-private package but since it is
useful generally, it makes more sense to migrate
this to power_manager ebuild to add it to all boards.

BUG=b:174742099
TEST=shellcheck, manual test in eve, lazor, morphius

Cq-Depend: chrome-internal:3437979, chrome-internal:3442787
Cq-Depend: chrome-internal:3443887, chrome-internal:3438640
Cq-Depend: chrome-internal:3437978, chromium:2573444
Cq-Depend: chromium:2581739, chromium:2581559
Change-Id: I6d1b61fd6b96d712d3840308d24891fbfbf0c762
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2569746
Tested-by: Puthikorn Voravootivat <puthik@chromium.org>
Commit-Queue: Puthikorn Voravootivat <puthik@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
diff --git a/power_manager/init/upstart/temp_logger.conf b/power_manager/init/upstart/temp_logger.conf
new file mode 100644
index 0000000..af69acd
--- /dev/null
+++ b/power_manager/init/upstart/temp_logger.conf
@@ -0,0 +1,32 @@
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+description     "Log temperature from internal sensors every minute"
+author          "chromium-os-dev@chromium.org"
+
+start on started system-services
+stop on stopping system-services
+respawn
+respawn limit 3 10  # if the job respawns 3 times in 10 seconds, stop trying.
+
+# Allow us to be killed as we are not critical to the system.
+oom score -100
+
+# Let the process crash if it grows too much.  "as" for "address space".
+# Currently it uses about 6.5 MB (by checking /proc/$PID/status).
+limit as 50000000 unlimited
+
+# Run the script in minijail minimalistic-mountns profile.
+# -b /dev/log for logger
+# -b /sys for /sys/class/thermal and /sys/class/powercap
+exec minijail0 \
+  -u power \
+  --profile=minimalistic-mountns \
+  --uts \
+  -e \
+  -l \
+  -p \
+  -b /dev/log \
+  -b /sys \
+  -- /usr/share/cros/init/temp_logger.sh
diff --git a/power_manager/tools/temp_logger.sh b/power_manager/tools/temp_logger.sh
new file mode 100644
index 0000000..c0bc548
--- /dev/null
+++ b/power_manager/tools/temp_logger.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Logger wrapper.
+log_message() {
+  logger -t temp_logger "$@"
+}
+
+# Output xx:yyC where xx is temp sensor id and yy is the actual temperature.
+get_sensor_temp() {
+  local zone="$1"
+  local tempc=0
+  local index=""
+  local tempstr=""
+
+  if [ ! -r "${zone}" ]; then
+    return
+  fi
+  tempc=$(($(cat "${zone}") / 1000))
+  index=$(echo "${zone}" | grep -o -E '[0-9]+')
+  tempstr=$(printf "%02d:%dC" "${index}" "${tempc}")
+  echo "${tempstr}"
+}
+
+# Glob all temp sensors sysfs and output temperatures with get_sensor_temp().
+get_all_sensor_temps() {
+  local logstr=""
+  local tempstr=""
+
+  for zone in /sys/class/thermal/thermal_zone*/temp; do
+    tempstr=$(get_sensor_temp "${zone}")
+    logstr="${logstr} ${tempstr}"
+  done
+
+  echo "${logstr}"
+}
+
+# Read PL1 from powercap sysfs (Intel only), output nothing otherwise.
+get_pl1() {
+  local pl1="/sys/class/powercap/intel-rapl:0/constraint_0_power_limit_uw"
+  local pl1uw=0
+  local pl1str=""
+
+  if [ ! -r "${pl1}" ]; then
+    return
+  fi
+  pl1uw=$(cat "${pl1}")
+  pl1str=$(printf "PL1:%.3fW" "$((pl1uw))e-6")
+  echo "${pl1str}"
+}
+
+main() {
+  if [ $# -ne 0 ]; then
+    echo "Usage: $0" >&2
+    exit 1
+  fi
+
+  while true; do
+    log_message "$(get_all_sensor_temps)" "$(get_pl1)"
+    sleep 60
+  done
+}
+
+main "$@"