PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cmds/incident_helper/src/parsers/CpuFreqParser.cpp

https://gitlab.com/SkyDragon-OSP/platform_frameworks_base
C++ | 90 lines | 60 code | 13 blank | 17 comment | 13 complexity | abb256b0b653260b6c6791b6d3b5e24e MD5 | raw file
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "incident_helper"
  17. #include <android/util/ProtoOutputStream.h>
  18. #include <unistd.h>
  19. #include "frameworks/base/core/proto/android/os/cpufreq.proto.h"
  20. #include "ih_util.h"
  21. #include "CpuFreqParser.h"
  22. using namespace android::os;
  23. status_t
  24. CpuFreqParser::Parse(const int in, const int out) const
  25. {
  26. Reader reader(in);
  27. string line;
  28. // parse header
  29. reader.readLine(&line);
  30. header_t header = parseHeader(line, TAB_DELIMITER);
  31. if (header.size() < 1) {
  32. fprintf(stderr, "Bad header: %s\n", line.c_str());
  33. return BAD_VALUE;
  34. }
  35. const int numCpus = (int)header.size() - 1;
  36. vector<pair<int, long long>> cpucores[numCpus];
  37. // parse freq and time
  38. while (reader.readLine(&line)) {
  39. if (line.empty()) continue;
  40. record_t record = parseRecord(line, TAB_DELIMITER);
  41. if (record.size() != header.size()) {
  42. fprintf(stderr, "Bad line: %s\n", line.c_str());
  43. continue;
  44. }
  45. int freq = toInt(record[0]);
  46. for (int i=0; i<numCpus; i++) {
  47. if (strcmp(record[i+1].c_str(), "N/A") == 0) {
  48. continue;
  49. }
  50. cpucores[i].push_back(make_pair(freq, toLongLong(record[i+1])));
  51. }
  52. }
  53. ProtoOutputStream proto;
  54. long jiffyHz = sysconf(_SC_CLK_TCK);
  55. proto.write(CpuFreqProto::JIFFY_HZ, (int)jiffyHz);
  56. for (int i=0; i<numCpus; i++) {
  57. uint64_t token = proto.start(CpuFreqProto::CPU_FREQS);
  58. proto.write(CpuFreqProto::Stats::CPU_NAME, header[i+1]);
  59. for (vector<pair<int, long long>>::iterator it = cpucores[i].begin(); it != cpucores[i].end(); it++) {
  60. uint64_t stateToken = proto.start(CpuFreqProto::Stats::TIMES);
  61. proto.write(CpuFreqProto::Stats::TimeInState::STATE_KHZ, it->first);
  62. proto.write(CpuFreqProto::Stats::TimeInState::TIME_JIFFY, it->second);
  63. proto.end(stateToken);
  64. }
  65. proto.end(token);
  66. }
  67. if (!reader.ok(&line)) {
  68. fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
  69. return -1;
  70. }
  71. if (!proto.flush(out)) {
  72. fprintf(stderr, "[%s]Error writing proto back\n", this->name.string());
  73. return -1;
  74. }
  75. fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.string(), proto.size());
  76. return NO_ERROR;
  77. }