blob: 0aa683b4bd0d84a2bb248303d56a22d7582922ed [file] [log] [blame]
Po-Hsien Wang00777b22019-04-24 16:37:09 -07001// 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
Po-Hsien Wang00777b22019-04-24 16:37:09 -07005#include <stdio.h>
6#include <stdlib.h>
7#include <time.h>
8#include <unistd.h>
9#include <chrono>
10#include <fstream>
11#include <sstream>
12#include <string>
13#include <vector>
14
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070015#include "utils.h"
16
Po-Hsien Wang00777b22019-04-24 16:37:09 -070017const std::string kTemperatureScript =
18 "/usr/local/autotest/bin/temperature.py --maximum";
19
20void PrintDateTime() {
21 time_t timer;
22 char buffer[26];
23 time(&timer);
24 struct tm* tm_info = localtime(&timer);
25 strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
26 LOG("# DateTime: %s", buffer)
27}
28
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080029std::string readFile(const std::string& filename) {
30 std::ifstream file(filename, std::ios::ate | std::ios::binary);
31
32 if (!file.is_open()) {
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070033 throw std::runtime_error("Failed to open " + filename);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080034 }
35 DEFER(file.close());
36
37 size_t fileSize = (size_t)file.tellg();
38 std::vector<char> buffer(fileSize);
39 file.seekg(0);
40 file.read(buffer.data(), fileSize);
41 return std::string(buffer.begin(), buffer.end());
42}
43
44// CreateShaderModule creates a shader module from a loaded SPIR-V code.
45vk::ShaderModule CreateShaderModule(const vk::Device& device,
46 std::string code) {
47 vk::ShaderModuleCreateInfo create_info;
48 create_info.setCodeSize(code.length());
49 create_info.setPCode(reinterpret_cast<const uint32_t*>(code.c_str()));
50 return device.createShaderModule(create_info);
51}
52
53// SavePPM saves data into filename in PPM format. It assumes data is in
54// R8G8B8A8_UNORM format.
55void SavePPM(std::string filename,
56 const char* data,
57 uint32_t width,
58 uint32_t height,
59 vk::DeviceSize row_pitch) {
60 std::ofstream file(filename, std::ios::binary | std::ios::out);
61 DEFER(file.close());
62
63 file << "P6\n" << width << "\n" << height << "\n" << 255 << std::endl;
64 for (uint32_t y = 0; y < height; y++) {
65 unsigned int* row = (unsigned int*)data;
66 for (uint32_t x = 0; x < width; x++) {
67 file.write((char*)row, 3);
68 row++;
69 }
70 data += row_pitch;
71 }
72}
73
Po-Hsien Wang00777b22019-04-24 16:37:09 -070074/* Execute a shell command and return its file descriptor for reading output. */
75/* @param command: command to be run. */
76/* @param result: the stdout of the command output. */
77/* @return true if the command is executed successfully. */
78bool ExecuteCommand(const std::string& kCommand,
79 std::string* result = nullptr) {
80 FILE* fd = popen(kCommand.c_str(), "r");
81 if (!fd) {
82 return false;
83 }
84
85 if (result) {
86 fseek(fd, 0, SEEK_END);
87 long size = ftell(fd);
88 fseek(fd, 0, SEEK_SET);
89 fread(&result[0], sizeof(char), size, fd);
90 }
91 return pclose(fd) == 0;
92}
93
94std::vector<std::string> SplitString(const std::string& kInput,
95 char delimiter) {
96 std::vector<std::string> result;
97 std::stringstream srcStream(kInput);
98
99 std::string token;
100 while (getline(srcStream, token, delimiter)) {
101 result.push_back(token);
102 }
103 return result;
104}
105
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700106bool IsItemInVector(const std::vector<std::string>& list,
107 const char* value,
108 bool empty_value = false) {
109 if (list.empty())
110 return empty_value;
111 return !(find(list.begin(), list.end(), std::string(value)) == list.end());
112}