Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "utils.h" |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <time.h> |
| 9 | #include <unistd.h> |
| 10 | #include <chrono> |
| 11 | #include <fstream> |
| 12 | #include <sstream> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | const std::string kTemperatureScript = |
| 17 | "/usr/local/autotest/bin/temperature.py --maximum"; |
| 18 | |
| 19 | void PrintDateTime() { |
| 20 | time_t timer; |
| 21 | char buffer[26]; |
| 22 | time(&timer); |
| 23 | struct tm* tm_info = localtime(&timer); |
| 24 | strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); |
| 25 | LOG("# DateTime: %s", buffer) |
| 26 | } |
| 27 | |
| 28 | /* Execute a shell command and return its file descriptor for reading output. */ |
| 29 | /* @param command: command to be run. */ |
| 30 | /* @param result: the stdout of the command output. */ |
| 31 | /* @return true if the command is executed successfully. */ |
| 32 | bool ExecuteCommand(const std::string& kCommand, |
| 33 | std::string* result = nullptr) { |
| 34 | FILE* fd = popen(kCommand.c_str(), "r"); |
| 35 | if (!fd) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | if (result) { |
| 40 | fseek(fd, 0, SEEK_END); |
| 41 | long size = ftell(fd); |
| 42 | fseek(fd, 0, SEEK_SET); |
| 43 | fread(&result[0], sizeof(char), size, fd); |
| 44 | } |
| 45 | return pclose(fd) == 0; |
| 46 | } |
| 47 | |
| 48 | std::vector<std::string> SplitString(const std::string& kInput, |
| 49 | char delimiter) { |
| 50 | std::vector<std::string> result; |
| 51 | std::stringstream srcStream(kInput); |
| 52 | |
| 53 | std::string token; |
| 54 | while (getline(srcStream, token, delimiter)) { |
| 55 | result.push_back(token); |
| 56 | } |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | // Returns currently measured temperature. |
| 61 | double GetMachineTemperature() { |
| 62 | std::string temp_str; |
| 63 | double temperature; |
| 64 | if (!ExecuteCommand(kTemperatureScript, &temp_str)) { |
| 65 | return 10000; |
| 66 | } |
| 67 | temperature = strtod(temp_str.c_str(), nullptr); |
| 68 | if (temperature < 10.0 || temperature > 150.0) { |
| 69 | DEBUG("Warning: ignoring temperature reading of %f'C.", temperature) |
| 70 | } |
| 71 | return temperature; |
| 72 | } |
| 73 | |
| 74 | // Waits up to timeout seconds to reach cold_temperature in Celsius. |
| 75 | // @param cold_temperature: the target temperature in celsius. |
| 76 | // @param timeout: the timeout to wait. |
| 77 | // @param temperature: the final temperature of the machine. |
| 78 | // @return: the total second of wait time. |
| 79 | double WaitForCoolMachine(double cold_temperature, |
| 80 | double timeout, |
| 81 | double* temperature) { |
| 82 | uint64_t start, now, end; |
| 83 | start = now = GetUTime(); |
| 84 | end = now + 1e6 * timeout; |
| 85 | do { |
| 86 | *temperature = GetMachineTemperature(); |
| 87 | if (*temperature < cold_temperature) |
| 88 | break; |
| 89 | sleep(1.0); |
| 90 | now = GetUTime(); |
| 91 | } while (now < end); |
| 92 | return 1.0e-6 * (now - start); |
| 93 | } |
| 94 | |
| 95 | bool IsItemInVector(const std::vector<std::string>& list, |
| 96 | const char* value, |
| 97 | bool empty_value = false) { |
| 98 | if (list.empty()) |
| 99 | return empty_value; |
| 100 | return !(find(list.begin(), list.end(), std::string(value)) == list.end()); |
| 101 | } |