This source file includes following definitions.
- sandbox_arch_
- GetProgramURL
- ResolveURL
- GetFileKeys
- ResolveKey
- HistogramTime
- HistogramSizeKB
- HistogramRatio
- HistogramKBPerSec
- HistogramEnumerateTranslationCache
- HistogramOptLevel
- BitcodeToNative
- expected_pexe_size_
- ReleaseTranslatedFD
- ReportNonPpapiError
- ReportPpapiError
- ExitWithError
- TranslateFinished
- NexeReadDidOpen
- OpenBitcodeStream
- BitcodeStreamDidOpen
- ResourceInfoWasRead
- ResourcesDidLoad
- NexeFdDidOpen
- BitcodeStreamDidFinish
- BitcodeStreamGotData
- GetCallback
- BitcodeGotCompiled
- GetCompileProgressCallback
- DoUMATimeMeasure
- GetUMATimeCallback
- GetCurrentProgress
- ObjectFileDidOpen
- RunTranslate
#include "ppapi/native_client/src/trusted/plugin/pnacl_coordinator.h"
#include <utility>
#include <vector>
#include "native_client/src/include/checked_cast.h"
#include "native_client/src/include/portability_io.h"
#include "native_client/src/shared/platform/nacl_check.h"
#include "native_client/src/trusted/service_runtime/include/sys/stat.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/private/ppb_uma_private.h"
#include "ppapi/native_client/src/trusted/plugin/manifest.h"
#include "ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.h"
#include "ppapi/native_client/src/trusted/plugin/plugin.h"
#include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
#include "ppapi/native_client/src/trusted/plugin/pnacl_translate_thread.h"
#include "ppapi/native_client/src/trusted/plugin/service_runtime.h"
#include "ppapi/native_client/src/trusted/plugin/temporary_file.h"
namespace plugin {
class PnaclManifest : public Manifest {
public:
PnaclManifest(const nacl::string& sandbox_arch)
: manifest_base_url_(PnaclUrls::GetBaseUrl()),
sandbox_arch_(sandbox_arch) { }
virtual ~PnaclManifest() { }
virtual bool GetProgramURL(nacl::string* full_url,
PnaclOptions* pnacl_options,
bool* uses_nonsfi_mode,
ErrorInfo* error_info) const {
UNREFERENCED_PARAMETER(full_url);
UNREFERENCED_PARAMETER(pnacl_options);
UNREFERENCED_PARAMETER(uses_nonsfi_mode);
UNREFERENCED_PARAMETER(error_info);
PLUGIN_PRINTF(("PnaclManifest does not contain a program\n"));
error_info->SetReport(PP_NACL_ERROR_MANIFEST_GET_NEXE_URL,
"pnacl manifest does not contain a program.");
return false;
}
virtual bool ResolveURL(const nacl::string& relative_url,
nacl::string* full_url,
ErrorInfo* error_info) const {
UNREFERENCED_PARAMETER(error_info);
*full_url = manifest_base_url_ + relative_url;
return true;
}
virtual bool GetFileKeys(std::set<nacl::string>* keys) const {
PLUGIN_PRINTF(("PnaclManifest does not support key enumeration\n"));
UNREFERENCED_PARAMETER(keys);
return false;
}
virtual bool ResolveKey(const nacl::string& key,
nacl::string* full_url,
PnaclOptions* pnacl_options,
ErrorInfo* error_info) const {
pnacl_options->set_translate(false);
const nacl::string kFilesPrefix = "files/";
size_t files_prefix_pos = key.find(kFilesPrefix);
if (files_prefix_pos == nacl::string::npos) {
error_info->SetReport(PP_NACL_ERROR_MANIFEST_RESOLVE_URL,
"key did not start with files/");
return false;
}
nacl::string key_basename = key.substr(kFilesPrefix.length());
return ResolveURL(sandbox_arch_ + "/" + key_basename,
full_url, error_info);
}
private:
NACL_DISALLOW_COPY_AND_ASSIGN(PnaclManifest);
nacl::string manifest_base_url_;
nacl::string sandbox_arch_;
};
namespace {
const int64_t kTimeLargeMin = 10;
const int64_t kTimeLargeMax = 720000;
const uint32_t kTimeLargeBuckets = 100;
const int32_t kSizeKBMin = 1;
const int32_t kSizeKBMax = 512*1024;
const uint32_t kSizeKBBuckets = 100;
const int32_t kRatioMin = 10;
const int32_t kRatioMax = 10*100;
const uint32_t kRatioBuckets = 100;
const int32_t kKBPSMin = 1;
const int32_t kKBPSMax = 30*1000;
const uint32_t kKBPSBuckets = 100;
void HistogramTime(pp::UMAPrivate& uma,
const nacl::string& name, int64_t ms) {
if (ms < 0) return;
uma.HistogramCustomTimes(name,
ms,
kTimeLargeMin, kTimeLargeMax,
kTimeLargeBuckets);
}
void HistogramSizeKB(pp::UMAPrivate& uma,
const nacl::string& name, int32_t kb) {
if (kb < 0) return;
uma.HistogramCustomCounts(name,
kb,
kSizeKBMin, kSizeKBMax,
kSizeKBBuckets);
}
void HistogramRatio(pp::UMAPrivate& uma,
const nacl::string& name, int64_t a, int64_t b) {
if (a < 0 || b <= 0) return;
uma.HistogramCustomCounts(name,
100 * a / b,
kRatioMin, kRatioMax,
kRatioBuckets);
}
void HistogramKBPerSec(pp::UMAPrivate& uma,
const nacl::string& name, double kb, double s) {
if (kb < 0.0 || s <= 0.0) return;
uma.HistogramCustomCounts(name,
static_cast<int64_t>(kb / s),
kKBPSMin, kKBPSMax,
kKBPSBuckets);
}
void HistogramEnumerateTranslationCache(pp::UMAPrivate& uma, bool hit) {
uma.HistogramEnumeration("NaCl.Perf.PNaClCache.IsHit",
hit, 2);
}
const int8_t kOptUnknown = 4;
void HistogramOptLevel(pp::UMAPrivate& uma, int8_t opt_level) {
if (opt_level < 0 || opt_level > 3) {
opt_level = kOptUnknown;
}
uma.HistogramEnumeration("NaCl.Options.PNaCl.OptLevel",
opt_level, kOptUnknown+1);
}
}
template<>
CallbackSource<FileStreamData>::~CallbackSource() {}
PnaclCoordinator* PnaclCoordinator::BitcodeToNative(
Plugin* plugin,
const nacl::string& pexe_url,
const PnaclOptions& pnacl_options,
const pp::CompletionCallback& translate_notify_callback) {
PLUGIN_PRINTF(("PnaclCoordinator::BitcodeToNative (plugin=%p, pexe=%s)\n",
static_cast<void*>(plugin), pexe_url.c_str()));
PnaclCoordinator* coordinator =
new PnaclCoordinator(plugin, pexe_url,
pnacl_options,
translate_notify_callback);
coordinator->pnacl_init_time_ = NaClGetTimeOfDayMicroseconds();
PLUGIN_PRINTF(("PnaclCoordinator::BitcodeToNative (manifest=%p, ",
reinterpret_cast<const void*>(coordinator->manifest_.get())));
int cpus = plugin->nacl_interface()->GetNumberOfProcessors();
coordinator->split_module_count_ = std::min(4, std::max(1, cpus));
coordinator->OpenBitcodeStream();
return coordinator;
}
PnaclCoordinator::PnaclCoordinator(
Plugin* plugin,
const nacl::string& pexe_url,
const PnaclOptions& pnacl_options,
const pp::CompletionCallback& translate_notify_callback)
: translate_finish_error_(PP_OK),
plugin_(plugin),
translate_notify_callback_(translate_notify_callback),
translation_finished_reported_(false),
manifest_(new PnaclManifest(plugin->nacl_interface()->GetSandboxArch())),
pexe_url_(pexe_url),
pnacl_options_(pnacl_options),
split_module_count_(1),
num_object_files_opened_(0),
is_cache_hit_(PP_FALSE),
error_already_reported_(false),
pnacl_init_time_(0),
pexe_size_(0),
pexe_bytes_compiled_(0),
expected_pexe_size_(-1) {
PLUGIN_PRINTF(("PnaclCoordinator::PnaclCoordinator (this=%p, plugin=%p)\n",
static_cast<void*>(this), static_cast<void*>(plugin)));
callback_factory_.Initialize(this);
}
PnaclCoordinator::~PnaclCoordinator() {
PLUGIN_PRINTF(("PnaclCoordinator::~PnaclCoordinator (this=%p, "
"translate_thread=%p\n",
static_cast<void*>(this), translate_thread_.get()));
if (translate_thread_.get() != NULL) {
translate_thread_->AbortSubprocesses();
}
if (!translation_finished_reported_) {
plugin_->nacl_interface()->ReportTranslationFinished(
plugin_->pp_instance(),
PP_FALSE);
}
translate_thread_.reset(NULL);
for (int i = 0; i < num_object_files_opened_; i++) {
delete obj_files_[i];
}
}
nacl::DescWrapper* PnaclCoordinator::ReleaseTranslatedFD() {
DCHECK(temp_nexe_file_ != NULL);
return temp_nexe_file_->release_read_wrapper();
}
void PnaclCoordinator::ReportNonPpapiError(PP_NaClError err_code,
const nacl::string& message) {
error_info_.SetReport(err_code, message);
ExitWithError();
}
void PnaclCoordinator::ReportPpapiError(PP_NaClError err_code,
int32_t pp_error,
const nacl::string& message) {
nacl::stringstream ss;
ss << "PnaclCoordinator: " << message << " (pp_error=" << pp_error << ").";
error_info_.SetReport(err_code, ss.str());
ExitWithError();
}
void PnaclCoordinator::ExitWithError() {
PLUGIN_PRINTF(("PnaclCoordinator::ExitWithError (error_code=%d, "
"message='%s')\n",
error_info_.error_code(),
error_info_.message().c_str()));
plugin_->ReportLoadError(error_info_);
callback_factory_.CancelAll();
if (!error_already_reported_) {
error_already_reported_ = true;
translation_finished_reported_ = true;
plugin_->nacl_interface()->ReportTranslationFinished(
plugin_->pp_instance(),
PP_FALSE);
translate_notify_callback_.Run(PP_ERROR_FAILED);
} else {
PLUGIN_PRINTF(("PnaclCoordinator::ExitWithError an earlier error was "
"already reported -- Skipping.\n"));
}
}
void PnaclCoordinator::TranslateFinished(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::TranslateFinished (pp_error=%"
NACL_PRId32 ")\n", pp_error));
if (translate_finish_error_ != PP_OK || pp_error != PP_OK) {
ExitWithError();
return;
}
if (ExpectedProgressKnown()) {
pexe_bytes_compiled_ = expected_pexe_size_;
plugin_->EnqueueProgressEvent(PP_NACL_EVENT_PROGRESS,
pexe_url_,
plugin::Plugin::LENGTH_IS_COMPUTABLE,
pexe_bytes_compiled_,
expected_pexe_size_);
}
HistogramOptLevel(plugin_->uma_interface(), pnacl_options_.opt_level());
HistogramKBPerSec(plugin_->uma_interface(),
"NaCl.Perf.PNaClLoadTime.CompileKBPerSec",
pexe_size_ / 1024.0,
translate_thread_->GetCompileTime() / 1000000.0);
HistogramSizeKB(plugin_->uma_interface(), "NaCl.Perf.Size.Pexe",
static_cast<int64_t>(pexe_size_ / 1024));
struct nacl_abi_stat stbuf;
struct NaClDesc* desc = temp_nexe_file_->read_wrapper()->desc();
int stat_ret;
if (0 != (stat_ret = (*((struct NaClDescVtbl const *) desc->base.vtbl)->
Fstat)(desc, &stbuf))) {
PLUGIN_PRINTF(("PnaclCoordinator::TranslateFinished can't stat nexe.\n"));
} else {
size_t nexe_size = stbuf.nacl_abi_st_size;
HistogramSizeKB(plugin_->uma_interface(),
"NaCl.Perf.Size.PNaClTranslatedNexe",
static_cast<int64_t>(nexe_size / 1024));
HistogramRatio(plugin_->uma_interface(),
"NaCl.Perf.Size.PexeNexeSizePct", pexe_size_, nexe_size);
}
int64_t total_time = NaClGetTimeOfDayMicroseconds() - pnacl_init_time_;
HistogramTime(plugin_->uma_interface(),
"NaCl.Perf.PNaClLoadTime.TotalUncachedTime",
total_time / NACL_MICROS_PER_MILLI);
HistogramKBPerSec(plugin_->uma_interface(),
"NaCl.Perf.PNaClLoadTime.TotalUncachedKBPerSec",
pexe_size_ / 1024.0,
total_time / 1000000.0);
temp_nexe_file_->Reset();
translation_finished_reported_ = true;
plugin_->nacl_interface()->ReportTranslationFinished(
plugin_->pp_instance(), PP_TRUE);
NexeReadDidOpen(PP_OK);
}
void PnaclCoordinator::NexeReadDidOpen(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::NexeReadDidOpen (pp_error=%"
NACL_PRId32 ")\n", pp_error));
if (pp_error != PP_OK) {
if (pp_error == PP_ERROR_FILENOTFOUND) {
ReportPpapiError(PP_NACL_ERROR_PNACL_CACHE_FETCH_NOTFOUND,
pp_error,
"Failed to open translated nexe (not found).");
return;
}
if (pp_error == PP_ERROR_NOACCESS) {
ReportPpapiError(PP_NACL_ERROR_PNACL_CACHE_FETCH_NOACCESS,
pp_error,
"Failed to open translated nexe (no access).");
return;
}
ReportPpapiError(PP_NACL_ERROR_PNACL_CACHE_FETCH_OTHER,
pp_error,
"Failed to open translated nexe.");
return;
}
translate_notify_callback_.Run(pp_error);
}
void PnaclCoordinator::OpenBitcodeStream() {
streaming_downloader_.reset(new FileDownloader());
streaming_downloader_->Initialize(plugin_);
streaming_downloader_->set_request_headers(
"Accept: application/x-pnacl, */*");
translate_thread_.reset(new PnaclTranslateThread());
if (translate_thread_ == NULL) {
ReportNonPpapiError(
PP_NACL_ERROR_PNACL_THREAD_CREATE,
"PnaclCoordinator: could not allocate translation thread.");
return;
}
pp::CompletionCallback cb =
callback_factory_.NewCallback(&PnaclCoordinator::BitcodeStreamDidOpen);
if (!streaming_downloader_->OpenStream(pexe_url_, cb, this)) {
ReportNonPpapiError(
PP_NACL_ERROR_PNACL_PEXE_FETCH_OTHER,
nacl::string("PnaclCoordinator: failed to open stream ") + pexe_url_);
return;
}
}
void PnaclCoordinator::BitcodeStreamDidOpen(int32_t pp_error) {
if (pp_error != PP_OK) {
BitcodeStreamDidFinish(pp_error);
TranslateFinished(pp_error);
return;
}
resources_.reset(new PnaclResources(plugin_, this, this->manifest_.get()));
CHECK(resources_ != NULL);
pp::CompletionCallback resource_info_read_cb =
callback_factory_.NewCallback(&PnaclCoordinator::ResourceInfoWasRead);
resources_->ReadResourceInfo(PnaclUrls::GetResourceInfoUrl(),
resource_info_read_cb);
}
void PnaclCoordinator::ResourceInfoWasRead(int32_t pp_error) {
PLUGIN_PRINTF(("PluginCoordinator::ResourceInfoWasRead (pp_error=%"
NACL_PRId32 ")\n", pp_error));
pp::CompletionCallback resources_cb =
callback_factory_.NewCallback(&PnaclCoordinator::ResourcesDidLoad);
resources_->StartLoad(resources_cb);
}
void PnaclCoordinator::ResourcesDidLoad(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::ResourcesDidLoad (pp_error=%"
NACL_PRId32 ")\n", pp_error));
if (pp_error != PP_OK) {
return;
}
nacl::string headers = streaming_downloader_->GetResponseHeaders();
NaClHttpResponseHeaders parser;
parser.Parse(headers);
temp_nexe_file_.reset(new TempFile(plugin_));
pp::CompletionCallback cb =
callback_factory_.NewCallback(&PnaclCoordinator::NexeFdDidOpen);
int32_t nexe_fd_err =
plugin_->nacl_interface()->GetNexeFd(
plugin_->pp_instance(),
streaming_downloader_->full_url().c_str(),
1,
pnacl_options_.opt_level(),
parser.GetHeader("last-modified").c_str(),
parser.GetHeader("etag").c_str(),
PP_FromBool(parser.CacheControlNoStore()),
plugin_->nacl_interface()->GetSandboxArch(),
"",
&is_cache_hit_,
temp_nexe_file_->existing_handle(),
cb.pp_completion_callback());
if (nexe_fd_err < PP_OK_COMPLETIONPENDING) {
ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, nexe_fd_err,
nacl::string("Call to GetNexeFd failed"));
}
}
void PnaclCoordinator::NexeFdDidOpen(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::NexeFdDidOpen (pp_error=%"
NACL_PRId32 ", hit=%d, handle=%d)\n", pp_error,
is_cache_hit_ == PP_TRUE,
*temp_nexe_file_->existing_handle()));
if (pp_error < PP_OK) {
ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP, pp_error,
nacl::string("GetNexeFd failed"));
return;
}
if (*temp_nexe_file_->existing_handle() == PP_kInvalidFileHandle) {
ReportNonPpapiError(
PP_NACL_ERROR_PNACL_CREATE_TEMP,
nacl::string(
"PnaclCoordinator: Got bad temp file handle from GetNexeFd"));
return;
}
HistogramEnumerateTranslationCache(plugin_->uma_interface(), is_cache_hit_);
if (is_cache_hit_ == PP_TRUE) {
streaming_downloader_.reset(NULL);
pp::CompletionCallback cb =
callback_factory_.NewCallback(&PnaclCoordinator::NexeReadDidOpen);
temp_nexe_file_->Open(cb, false);
} else {
for (int i = 0; i < split_module_count_; i++) {
obj_files_.push_back(new TempFile(plugin_));
pp::CompletionCallback obj_cb =
callback_factory_.NewCallback(&PnaclCoordinator::ObjectFileDidOpen);
obj_files_[i]->Open(obj_cb, true);
}
invalid_desc_wrapper_.reset(plugin_->wrapper_factory()->MakeInvalid());
pp::CompletionCallback finish_cb = callback_factory_.NewCallback(
&PnaclCoordinator::BitcodeStreamDidFinish);
streaming_downloader_->FinishStreaming(finish_cb);
}
}
void PnaclCoordinator::BitcodeStreamDidFinish(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamDidFinish (pp_error=%"
NACL_PRId32 ")\n", pp_error));
if (pp_error != PP_OK) {
translate_finish_error_ = pp_error;
if (pp_error == PP_ERROR_ABORTED) {
error_info_.SetReport(PP_NACL_ERROR_PNACL_PEXE_FETCH_ABORTED,
"PnaclCoordinator: pexe load failed (aborted).");
}
if (pp_error == PP_ERROR_NOACCESS) {
error_info_.SetReport(PP_NACL_ERROR_PNACL_PEXE_FETCH_NOACCESS,
"PnaclCoordinator: pexe load failed (no access).");
} else {
nacl::stringstream ss;
ss << "PnaclCoordinator: pexe load failed (pp_error=" << pp_error << ").";
error_info_.SetReport(PP_NACL_ERROR_PNACL_PEXE_FETCH_OTHER, ss.str());
}
translate_thread_->AbortSubprocesses();
} else {
HistogramRatio(plugin_->uma_interface(),
"NaCl.Perf.PNaClLoadTime.PctCompiledWhenFullyDownloaded",
pexe_bytes_compiled_, pexe_size_);
}
}
void PnaclCoordinator::BitcodeStreamGotData(int32_t pp_error,
FileStreamData data) {
PLUGIN_PRINTF(("PnaclCoordinator::BitcodeStreamGotData (pp_error=%"
NACL_PRId32 ", data=%p)\n", pp_error, data ? &(*data)[0] : 0));
DCHECK(translate_thread_.get());
translate_thread_->PutBytes(data, pp_error);
if (data && pp_error > 0) {
pexe_size_ += pp_error;
}
}
StreamCallback PnaclCoordinator::GetCallback() {
return callback_factory_.NewCallbackWithOutput(
&PnaclCoordinator::BitcodeStreamGotData);
}
void PnaclCoordinator::BitcodeGotCompiled(int32_t pp_error,
int64_t bytes_compiled) {
DCHECK(pp_error == PP_OK);
pexe_bytes_compiled_ += bytes_compiled;
if (!ExpectedProgressKnown()) {
int64_t amount_downloaded;
streaming_downloader_->GetDownloadProgress(&amount_downloaded,
&expected_pexe_size_);
}
if (ExpectedProgressKnown()) {
if (!ShouldDelayProgressEvent()) {
plugin_->EnqueueProgressEvent(PP_NACL_EVENT_PROGRESS,
pexe_url_,
plugin::Plugin::LENGTH_IS_COMPUTABLE,
pexe_bytes_compiled_,
expected_pexe_size_);
}
} else {
plugin_->EnqueueProgressEvent(PP_NACL_EVENT_PROGRESS,
pexe_url_,
plugin::Plugin::LENGTH_IS_NOT_COMPUTABLE,
pexe_bytes_compiled_,
expected_pexe_size_);
}
}
pp::CompletionCallback PnaclCoordinator::GetCompileProgressCallback(
int64_t bytes_compiled) {
return callback_factory_.NewCallback(&PnaclCoordinator::BitcodeGotCompiled,
bytes_compiled);
}
void PnaclCoordinator::DoUMATimeMeasure(int32_t pp_error,
const nacl::string& event_name,
int64_t microsecs) {
DCHECK(pp_error == PP_OK);
HistogramTime(
plugin_->uma_interface(), event_name, microsecs / NACL_MICROS_PER_MILLI);
}
pp::CompletionCallback PnaclCoordinator::GetUMATimeCallback(
const nacl::string& event_name, int64_t microsecs) {
return callback_factory_.NewCallback(&PnaclCoordinator::DoUMATimeMeasure,
event_name,
microsecs);
}
void PnaclCoordinator::GetCurrentProgress(int64_t* bytes_loaded,
int64_t* bytes_total) {
*bytes_loaded = pexe_bytes_compiled_;
*bytes_total = expected_pexe_size_;
}
void PnaclCoordinator::ObjectFileDidOpen(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::ObjectFileDidOpen (pp_error=%"
NACL_PRId32 ")\n", pp_error));
if (pp_error != PP_OK) {
ReportPpapiError(PP_NACL_ERROR_PNACL_CREATE_TEMP,
pp_error,
"Failed to open scratch object file.");
return;
}
num_object_files_opened_++;
if (num_object_files_opened_ == split_module_count_) {
pp::CompletionCallback cb =
callback_factory_.NewCallback(&PnaclCoordinator::RunTranslate);
temp_nexe_file_->Open(cb, true);
}
}
void PnaclCoordinator::RunTranslate(int32_t pp_error) {
PLUGIN_PRINTF(("PnaclCoordinator::RunTranslate (pp_error=%"
NACL_PRId32 ")\n", pp_error));
pp::CompletionCallback report_translate_finished =
callback_factory_.NewCallback(&PnaclCoordinator::TranslateFinished);
CHECK(translate_thread_ != NULL);
translate_thread_->RunTranslate(report_translate_finished,
manifest_.get(),
&obj_files_,
temp_nexe_file_.get(),
invalid_desc_wrapper_.get(),
&error_info_,
resources_.get(),
&pnacl_options_,
this,
plugin_);
}
}