-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add measuring the CPU usage for testing the performance. b/302715613
- Loading branch information
Showing
11 changed files
with
556 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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/logging.h" | ||
#include "cobalt/base/statistics.h" | ||
#include "starboard/extension/player_perf.h" | ||
#include "starboard/once.h" | ||
#include "starboard/system.h" | ||
|
||
namespace cobalt { | ||
namespace media { | ||
|
||
namespace { | ||
|
||
const int64_t kMaxSamples = 256; | ||
|
||
class StatisticsWrapper { | ||
public: | ||
static StatisticsWrapper* GetInstance(); | ||
base::Statistics<double, int, 1024> cpu_usage{"Media.CPUUsage"}; | ||
}; | ||
|
||
} // namespace | ||
|
||
SB_ONCE_INITIALIZE_FUNCTION(StatisticsWrapper, StatisticsWrapper::GetInstance); | ||
|
||
void WebMediaPlayerPerf::Start() { | ||
task_runner_->PostTask(FROM_HERE, | ||
base::Bind(&WebMediaPlayerPerf::StartTask, this)); | ||
} | ||
|
||
void WebMediaPlayerPerf::StartTask() { | ||
DCHECK(task_runner_->BelongsToCurrentThread()); | ||
timer_.Start(FROM_HERE, base::Seconds(1), this, | ||
&WebMediaPlayerPerf::GetPlatformIndependentCPUUsage); | ||
} | ||
|
||
void WebMediaPlayerPerf::Stop() { | ||
task_runner_->PostTask(FROM_HERE, | ||
base::Bind(&WebMediaPlayerPerf::StopTask, this)); | ||
} | ||
|
||
void WebMediaPlayerPerf::StopTask() { | ||
DCHECK(task_runner_->BelongsToCurrentThread()); | ||
timer_.Stop(); | ||
} | ||
|
||
void WebMediaPlayerPerf::GetPlatformIndependentCPUUsage() { | ||
DCHECK(task_runner_->BelongsToCurrentThread()); | ||
|
||
const StarboardExtensionPlayerPerfApi* player_perf_extension = | ||
static_cast<const StarboardExtensionPlayerPerfApi*>( | ||
SbSystemGetExtension(kStarboardExtensionPlayerPerfName)); | ||
if (player_perf_extension && | ||
strcmp(player_perf_extension->name, kStarboardExtensionPlayerPerfName) == | ||
0 && | ||
player_perf_extension->version >= 1) { | ||
double cpu_usage_perf = | ||
player_perf_extension->GetPlatformIndependentCPUUsage(); | ||
if (cpu_usage_perf == 0.0 && data_count_ == 0) { | ||
LOG(ERROR) << "Brown first sample, skip"; | ||
return; | ||
} | ||
StatisticsWrapper::GetInstance()->cpu_usage.AddSample(cpu_usage_perf, 1); | ||
data_count_ += 1; | ||
if (data_count_ == kMaxSamples) { | ||
LOG(ERROR) << "Brown CPU usage statistics (%): brand name: " | ||
<< sb_system_property_brand_name_ | ||
<< ", model name: " << sb_system_property_model_name_ | ||
<< ", platform name: " << sb_system_property_platform_name_ | ||
<< ", min: " | ||
<< StatisticsWrapper::GetInstance()->cpu_usage.min() | ||
<< "%, median: " | ||
<< StatisticsWrapper::GetInstance()->cpu_usage.GetMedian() | ||
<< "%, average: " | ||
<< StatisticsWrapper::GetInstance()->cpu_usage.average() | ||
<< "%, max: " | ||
<< StatisticsWrapper::GetInstance()->cpu_usage.max() << "%"; | ||
} | ||
} | ||
} | ||
|
||
std::string WebMediaPlayerPerf::GetSbSystemProperty( | ||
SbSystemPropertyId property_id) { | ||
char property[1024] = {0}; | ||
std::string result = ""; | ||
if (!SbSystemGetProperty(property_id, property, | ||
SB_ARRAY_SIZE_INT(property))) { | ||
DLOG(FATAL) << "Failed to get kSbSystemPropertyPlatformName."; | ||
return result; | ||
} | ||
result = property; | ||
return result; | ||
} | ||
|
||
WebMediaPlayerPerf::WebMediaPlayerPerf( | ||
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | ||
: task_runner_(task_runner), | ||
data_count_(0), | ||
sb_system_property_brand_name_( | ||
GetSbSystemProperty(kSbSystemPropertyBrandName)), | ||
sb_system_property_model_name_( | ||
GetSbSystemProperty(kSbSystemPropertyModelName)), | ||
sb_system_property_platform_name_( | ||
GetSbSystemProperty(kSbSystemPropertyPlatformName)) {} | ||
|
||
WebMediaPlayerPerf::~WebMediaPlayerPerf() {} | ||
|
||
} // namespace media | ||
} // namespace cobalt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 <string> | ||
#include <vector> | ||
|
||
#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/common/system_property.h" | ||
#include "starboard/types.h" | ||
|
||
namespace cobalt { | ||
namespace media { | ||
|
||
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 GetPlatformIndependentCPUUsage(); | ||
std::string GetSbSystemProperty(SbSystemPropertyId property_id); | ||
|
||
// Message loop used to execute pipeline tasks. It is thread-safe. | ||
scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | ||
base::RepeatingTimer timer_; | ||
|
||
// TODO(borongchen): ensure when to log CPU statistics, as each video segment | ||
// will Play() once | ||
int data_count_; | ||
std::string sb_system_property_brand_name_; | ||
std::string sb_system_property_model_name_; | ||
std::string sb_system_property_platform_name_; | ||
}; | ||
|
||
} // namespace media | ||
} // namespace cobalt | ||
|
||
#endif // COBALT_MEDIA_PLAYER_WEB_MEDIA_PLAYER_PERF_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.