Skip to content

Commit

Permalink
[media] Measuring CPU usage
Browse files Browse the repository at this point in the history
Add measuring the CPU usage for testing the performance.

b/302715613
  • Loading branch information
borongc committed Jan 28, 2024
1 parent c39f97a commit 19a7c5d
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cobalt/media/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ component("media") {
"media_module.h",
"player/web_media_player_impl.cc",
"player/web_media_player_impl.h",
"player/web_media_player_perf.cc",
"player/web_media_player_perf.h",
"player/web_media_player_proxy.cc",
"player/web_media_player_proxy.h",
"progressive/avc_access_unit.cc",
Expand Down
10 changes: 10 additions & 0 deletions cobalt/media/player/web_media_player_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// found in the LICENSE file.
#include "cobalt/media/player/web_media_player_impl.h"

#include <unistd.h>

#include <cmath>
#include <cstring>
#include <limits>
Expand Down Expand Up @@ -116,6 +118,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
#endif // SB_API_VERSION >= 15
::media::MediaLog* const media_log)
: pipeline_thread_("media_pipeline"),
perf_thread_("cpu_utilization_perf"),
network_state_(WebMediaPlayer::kNetworkStateEmpty),
ready_state_(WebMediaPlayer::kReadyStateHaveNothing),
main_loop_(base::MessageLoop::current()),
Expand Down Expand Up @@ -149,6 +152,9 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
#endif // SB_API_VERSION >= 15
media_log_, &media_metrics_provider_, decode_target_provider_.get());

perf_thread_.Start();
web_media_player_perf_ = new WebMediaPlayerPerf(perf_thread_.task_runner());

Check warning on line 156 in cobalt/media/player/web_media_player_impl.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_impl.cc#L155-L156

Added lines #L155 - L156 were not covered by tests

// Also we want to be notified of |main_loop_| destruction.
main_loop_->AddDestructionObserver(this);

Expand Down Expand Up @@ -180,6 +186,7 @@ WebMediaPlayerImpl::~WebMediaPlayerImpl() {
main_loop_->RemoveDestructionObserver(this);
}
pipeline_thread_.Stop();
perf_thread_.Stop();

Check warning on line 189 in cobalt/media/player/web_media_player_impl.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_impl.cc#L189

Added line #L189 was not covered by tests
}

namespace {
Expand Down Expand Up @@ -845,6 +852,7 @@ void WebMediaPlayerImpl::StartPipeline(::media::Demuxer* demuxer) {
BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOutputModeChanged),
BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnContentSizeChanged),
GetClient()->MaxVideoCapabilities(), GetClient()->MaxVideoInputSize());
web_media_player_perf_->Start();

Check warning on line 855 in cobalt/media/player/web_media_player_impl.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_impl.cc#L855

Added line #L855 was not covered by tests
}

void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
Expand Down Expand Up @@ -903,6 +911,8 @@ void WebMediaPlayerImpl::Destroy() {
proxy_->AbortDataSource();
}

web_media_player_perf_->Stop();

Check warning on line 914 in cobalt/media/player/web_media_player_impl.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_impl.cc#L914

Added line #L914 was not covered by tests

// Make sure to kill the pipeline so there's no more media threads running.
// Note: stopping the pipeline might block for a long time.
base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC,
Expand Down
4 changes: 4 additions & 0 deletions cobalt/media/player/web_media_player_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "cobalt/media/base/sbplayer_interface.h"
#include "cobalt/media/player/web_media_player.h"
#include "cobalt/media/player/web_media_player_delegate.h"
#include "cobalt/media/player/web_media_player_perf.h"
#include "third_party/chromium/media/base/demuxer.h"
#include "third_party/chromium/media/base/eme_constants.h"
#include "third_party/chromium/media/base/media_log.h"
Expand Down Expand Up @@ -240,6 +241,7 @@ class WebMediaPlayerImpl : public WebMediaPlayer,
void OnContentSizeChanged();

base::Thread pipeline_thread_;
base::Thread perf_thread_;

// TODO(hclam): get rid of these members and read from the pipeline directly.
NetworkState network_state_;
Expand All @@ -251,6 +253,8 @@ class WebMediaPlayerImpl : public WebMediaPlayer,

scoped_refptr<Pipeline> pipeline_;

scoped_refptr<WebMediaPlayerPerf> web_media_player_perf_;

// The currently selected key system. Empty string means that no key system
// has been selected.
std::string current_key_system_;
Expand Down
215 changes: 215 additions & 0 deletions cobalt/media/player/web_media_player_perf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/media/player/web_media_player_perf.h"

#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "starboard/common/system_property.h"
#include "starboard/system.h"

namespace cobalt {
namespace media {

const char kProcDir[] = "/proc";

const char kStatFile[] = "stat";

void WebMediaPlayerPerf::Start() {
LOG(ERROR) << "Brown WebMediaPlayerPerf::Start";
std::vector<SbSystemPropertyId> sb_system_property_ids{
kSbSystemPropertyBrandName, kSbSystemPropertyModelName,
kSbSystemPropertyPlatformName};
for (auto property_id : sb_system_property_ids) {
char property[512];
if (!SbSystemGetProperty(property_id, property,
SB_ARRAY_SIZE_INT(property))) {
DLOG(FATAL) << "Failed to get kSbSystemPropertyPlatformName.";

Check warning on line 43 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L34-L43

Added lines #L34 - L43 were not covered by tests
}
LOG(ERROR) << "Brown property " << property;

Check warning on line 45 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L45

Added line #L45 was not covered by tests
}

task_runner_->PostTask(FROM_HERE,
base::Bind(&WebMediaPlayerPerf::StartTask, this));

Check warning on line 49 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L48-L49

Added lines #L48 - L49 were not covered by tests
}

void WebMediaPlayerPerf::StartTask() {
DCHECK(task_runner_->BelongsToCurrentThread());
LOG(ERROR) << "Brown WebMediaPlayerPerf::StartTask " << this << "/"
<< process_pid_;

Check warning on line 55 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L52-L55

Added lines #L52 - L55 were not covered by tests

timer_.Start(FROM_HERE, base::Seconds(1), this,
&WebMediaPlayerPerf::GetCpuUsage);

Check warning on line 58 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L57-L58

Added lines #L57 - L58 were not covered by tests
}

void WebMediaPlayerPerf::Stop() {
LOG(ERROR) << "Brown WebMediaPlayerPerf::Stop";

Check warning on line 62 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L61-L62

Added lines #L61 - L62 were not covered by tests

task_runner_->PostTask(FROM_HERE,
base::Bind(&WebMediaPlayerPerf::StopTask, this));

Check warning on line 65 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L64-L65

Added lines #L64 - L65 were not covered by tests
}

void WebMediaPlayerPerf::StopTask() {
DCHECK(task_runner_->BelongsToCurrentThread());
LOG(ERROR) << "Brown WebMediaPlayerPerf::StopTask";
timer_.Stop();

Check warning on line 71 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L68-L71

Added lines #L68 - L71 were not covered by tests
}

void WebMediaPlayerPerf::GetCpuUsage() {
DCHECK(task_runner_->BelongsToCurrentThread());
LOG(ERROR) << "Brown GetCpuUsage pid: " << process_pid_
<< ", usage: " << GetPlatformIndependentCPUUsage() << "%";

Check warning on line 77 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L74-L77

Added lines #L74 - L77 were not covered by tests
}

base::FilePath WebMediaPlayerPerf::GetProcPidDir(pid_t pid) {
return base::FilePath(kProcDir).Append(base::IntToString(pid));

Check warning on line 81 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L80-L81

Added lines #L80 - L81 were not covered by tests
}

bool WebMediaPlayerPerf::ReadProcFile(const base::FilePath& file,
std::string* buffer) {
buffer->clear();

Check warning on line 86 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L85-L86

Added lines #L85 - L86 were not covered by tests
// Synchronously reading files in /proc is safe.
base::ThreadRestrictions::ScopedAllowIO allow_io;

Check warning on line 88 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L88

Added line #L88 was not covered by tests

if (!base::ReadFileToString(file, buffer)) {
DLOG(WARNING) << "Failed to read " << file.MaybeAsASCII();
return false;

Check warning on line 92 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L90-L92

Added lines #L90 - L92 were not covered by tests
}
return !buffer->empty();

Check warning on line 94 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L94

Added line #L94 was not covered by tests
}

bool WebMediaPlayerPerf::ReadProcStats(pid_t pid, std::string* buffer) {
base::FilePath stat_file = GetProcPidDir(pid).Append(kStatFile);
return ReadProcFile(stat_file, buffer);

Check warning on line 99 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L97-L99

Added lines #L97 - L99 were not covered by tests
}

bool WebMediaPlayerPerf::ParseProcStats(const std::string& stats_data,
std::vector<std::string>* proc_stats) {

Check warning on line 103 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L103

Added line #L103 was not covered by tests
// |stats_data| may be empty if the process disappeared somehow.
// e.g. http://crbug.com/145811
if (stats_data.empty()) return false;

Check warning on line 106 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L106

Added line #L106 was not covered by tests

// The stat file is formatted as:
// pid (process name) data1 data2 .... dataN
// Look for the closing paren by scanning backwards, to avoid being fooled by
// processes with ')' in the name.
size_t open_parens_idx = stats_data.find(" (");
size_t close_parens_idx = stats_data.rfind(") ");
if (open_parens_idx == std::string::npos ||
close_parens_idx == std::string::npos ||
open_parens_idx > close_parens_idx) {
DLOG(WARNING) << "Failed to find matched parens in '" << stats_data << "'";
NOTREACHED();
return false;

Check warning on line 119 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L112-L119

Added lines #L112 - L119 were not covered by tests
}
open_parens_idx++;

Check warning on line 121 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L121

Added line #L121 was not covered by tests

proc_stats->clear();

Check warning on line 123 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L123

Added line #L123 was not covered by tests
// PID.
proc_stats->push_back(stats_data.substr(0, open_parens_idx));

Check warning on line 125 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L125

Added line #L125 was not covered by tests
// Process name without parentheses.
proc_stats->push_back(stats_data.substr(
open_parens_idx + 1, close_parens_idx - (open_parens_idx + 1)));

Check warning on line 128 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L127-L128

Added lines #L127 - L128 were not covered by tests

// Split the rest.
std::vector<std::string> other_stats =
SplitString(stats_data.substr(close_parens_idx + 2), " ",
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
for (size_t i = 0; i < other_stats.size(); ++i)
proc_stats->push_back(other_stats[i]);
return true;

Check warning on line 136 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L131-L136

Added lines #L131 - L136 were not covered by tests
}

int64_t WebMediaPlayerPerf::GetProcStatsFieldAsInt64(
const std::vector<std::string>& proc_stats, ProcStatsFields field_num) {
DCHECK_GE(field_num, VM_PPID);
CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());

Check warning on line 142 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L140-L142

Added lines #L140 - L142 were not covered by tests

int64_t value;
return base::StringToInt64(proc_stats[field_num], &value) ? value : 0;

Check warning on line 145 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L144-L145

Added lines #L144 - L145 were not covered by tests
}

// Get the total CPU of a single process. Return value is number of jiffies
// on success or -1 on error.
int64_t WebMediaPlayerPerf::GetProcessCPU(pid_t pid) {
std::string buffer;
std::vector<std::string> proc_stats;
if (!ReadProcStats(pid, &buffer) || !ParseProcStats(buffer, &proc_stats)) {
return -1;

Check warning on line 154 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L150-L154

Added lines #L150 - L154 were not covered by tests
}

int64_t total_cpu = GetProcStatsFieldAsInt64(proc_stats, VM_UTIME) +
GetProcStatsFieldAsInt64(proc_stats, VM_STIME);
return total_cpu;

Check warning on line 159 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L157-L159

Added lines #L157 - L159 were not covered by tests
}

base::TimeDelta WebMediaPlayerPerf::ClockTicksToTimeDelta(int64_t clock_ticks) {

Check warning on line 162 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L162

Added line #L162 was not covered by tests
// This queries the /proc-specific scaling factor which is
// conceptually the system hertz. To dump this value on another
// system, try
// od -t dL /proc/self/auxv
// and look for the number after 17 in the output; mine is
// 0000040 17 100 3 134512692
// which means the answer is 100.
// It may be the case that this value is always 100.
static const int64_t kHertz = sysconf(_SC_CLK_TCK);

Check warning on line 171 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L171

Added line #L171 was not covered by tests

return base::Microseconds(base::Time::kMicrosecondsPerSecond * clock_ticks /
kHertz);

Check warning on line 174 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L173-L174

Added lines #L173 - L174 were not covered by tests
}

base::TimeDelta WebMediaPlayerPerf::GetCumulativeCPUUsage() {
return ClockTicksToTimeDelta(GetProcessCPU(process_pid_));

Check warning on line 178 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L177-L178

Added lines #L177 - L178 were not covered by tests
}

double WebMediaPlayerPerf::GetPlatformIndependentCPUUsage() {
base::TimeDelta cumulative_cpu = GetCumulativeCPUUsage();
base::TimeTicks time = base::TimeTicks::Now();

Check warning on line 183 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L181-L183

Added lines #L181 - L183 were not covered by tests

if (last_cumulative_cpu_.is_zero()) {

Check warning on line 185 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L185

Added line #L185 was not covered by tests
// First call, just set the last values.
last_cumulative_cpu_ = cumulative_cpu;
last_cpu_time_ = time;
return 0;

Check warning on line 189 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L187-L189

Added lines #L187 - L189 were not covered by tests
}

base::TimeDelta system_time_delta = cumulative_cpu - last_cumulative_cpu_;
base::TimeDelta time_delta = time - last_cpu_time_;
DCHECK(!time_delta.is_zero());
if (time_delta.is_zero()) return 0;

Check warning on line 195 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L192-L195

Added lines #L192 - L195 were not covered by tests

last_cumulative_cpu_ = cumulative_cpu;
last_cpu_time_ = time;

Check warning on line 198 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L197-L198

Added lines #L197 - L198 were not covered by tests

return 100.0 * system_time_delta.InMicrosecondsF() /
time_delta.InMicrosecondsF();

Check warning on line 201 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L200-L201

Added lines #L200 - L201 were not covered by tests
}

WebMediaPlayerPerf::WebMediaPlayerPerf(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
: task_runner_(task_runner), process_pid_(getpid()) {
LOG(ERROR) << "Brown WebMediaPlayerPerf() " << this << "/" << process_pid_;

Check warning on line 207 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L206-L207

Added lines #L206 - L207 were not covered by tests
}

WebMediaPlayerPerf::~WebMediaPlayerPerf() {
LOG(ERROR) << "Brown ~WebMediaPlayerPerf() " << this << "/" << process_pid_;

Check warning on line 211 in cobalt/media/player/web_media_player_perf.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/media/player/web_media_player_perf.cc#L210-L211

Added lines #L210 - L211 were not covered by tests
}

} // namespace media
} // namespace cobalt
97 changes: 97 additions & 0 deletions cobalt/media/player/web_media_player_perf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_MEDIA_PLAYER_WEB_MEDIA_PLAYER_PERF_H_
#define COBALT_MEDIA_PLAYER_WEB_MEDIA_PLAYER_PERF_H_

#include <unistd.h>

#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/task_runner.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "starboard/types.h"

namespace cobalt {
namespace media {

// Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
// If the ordering ever changes, carefully review functions that use these
// values.
enum ProcStatsFields {
VM_COMM = 1, // Filename of executable, without parentheses.
VM_STATE = 2, // Letter indicating the state of the process.
VM_PPID = 3, // PID of the parent.
VM_PGRP = 4, // Process group id.
VM_MINFLT = 9, // Minor page fault count excluding children.
VM_MAJFLT = 11, // Major page fault count excluding children.
VM_UTIME = 13, // Time scheduled in user mode in clock ticks.
VM_STIME = 14, // Time scheduled in kernel mode in clock ticks.
VM_NUMTHREADS = 19, // Number of threads.
VM_STARTTIME = 21, // The time the process started in clock ticks.
VM_VSIZE = 22, // Virtual memory size in bytes.
VM_RSS = 23, // Resident Set Size in pages.
};

class WebMediaPlayerPerf
: public base::RefCountedThreadSafe<WebMediaPlayerPerf> {
public:
// Constructs a measure pipeline that will execute on |task_runner|.
WebMediaPlayerPerf(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
~WebMediaPlayerPerf();

void Start();
void StartTask();
void Stop();
void StopTask();

private:
// Refer base/process/process_metrics.h for GetPlatformIndependentCPUUsage()
void GetCpuUsage();
int64_t GetProcessCPU(pid_t pid);
base::FilePath GetProcPidDir(pid_t pid);
bool ReadProcFile(const base::FilePath& file, std::string* buffer);
bool ReadProcStats(pid_t pid, std::string* buffer);
bool ParseProcStats(const std::string& stats_data,
std::vector<std::string>* proc_stats);
int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
ProcStatsFields field_num);
base::TimeDelta ClockTicksToTimeDelta(int64_t clock_ticks);
base::TimeDelta GetCumulativeCPUUsage();
double GetPlatformIndependentCPUUsage();

// Message loop used to execute pipeline tasks. It is thread-safe.
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::RepeatingTimer timer_;

pid_t process_pid_;
char sys_pid_stat_[32] = {};

// Used to store the previous times and CPU usage counts so we can
// compute the CPU usage between calls.
base::TimeTicks last_cpu_time_;
base::TimeDelta last_cumulative_cpu_;
};

} // namespace media
} // namespace cobalt

#endif // COBALT_MEDIA_PLAYER_WEB_MEDIA_PLAYER_PERF_H_

0 comments on commit 19a7c5d

Please sign in to comment.