diff options
author | bsy@google.com <bsy@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-19 14:46:28 +0000 |
---|---|---|
committer | bsy@google.com <bsy@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-19 14:46:28 +0000 |
commit | 4d82ea811913c9aac247082502b9ebf47fde3229 (patch) | |
tree | b3c293427615e5474848bab871d493e4f6002a98 /chrome | |
parent | 38f428f10f6e3dca2ff875e663f614aa56fc099f (diff) | |
download | chromium_src-4d82ea811913c9aac247082502b9ebf47fde3229.zip chromium_src-4d82ea811913c9aac247082502b9ebf47fde3229.tar.gz chromium_src-4d82ea811913c9aac247082502b9ebf47fde3229.tar.bz2 |
Set up NaClChromeMainArgs number_of_cores member so apps can size threadpools appropriately
The outer sandbox on Linux and OSX was preventing
sysconf(_SC_NPROCESSORS_ONLN) from succeeding, so that NaCl
applications that need the number of threads were getting a
bogus value (-1 when using newlib, and 1 when using glibc).
TEST= run_sysconf_nprocessors_onln_test
BUG= 176522
Review URL: https://chromiumcodereview.appspot.com/14238013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195190 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/nacl/nacl_helper_linux.cc | 12 | ||||
-rw-r--r-- | chrome/nacl/nacl_listener.cc | 8 | ||||
-rw-r--r-- | chrome/nacl/nacl_listener.h | 15 | ||||
-rw-r--r-- | chrome/nacl/nacl_main.cc | 9 | ||||
-rw-r--r-- | chrome/test/data/nacl/nacl_test_data.gyp | 16 | ||||
-rw-r--r-- | chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc | 108 | ||||
-rw-r--r-- | chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html | 59 | ||||
-rw-r--r-- | chrome/test/nacl/nacl_browsertest.cc | 39 |
8 files changed, 262 insertions, 4 deletions
diff --git a/chrome/nacl/nacl_helper_linux.cc b/chrome/nacl/nacl_helper_linux.cc index 53c2fb30..a066db7 100644 --- a/chrome/nacl/nacl_helper_linux.cc +++ b/chrome/nacl/nacl_helper_linux.cc @@ -40,7 +40,8 @@ namespace { // if (!child) { // Note: this code doesn't attempt to support SELINUX or the SECCOMP sandbox. void BecomeNaClLoader(const std::vector<int>& child_fds, - size_t prereserved_sandbox_size) { + size_t prereserved_sandbox_size, + int number_of_cores) { VLOG(1) << "NaCl loader: setting up IPC descriptor"; // don't need zygote FD any more if (HANDLE_EINTR(close(kNaClZygoteDescriptor)) != 0) @@ -51,6 +52,7 @@ void BecomeNaClLoader(const std::vector<int>& child_fds, MessageLoopForIO main_message_loop; NaClListener listener; listener.set_prereserved_sandbox_size(prereserved_sandbox_size); + listener.set_number_of_cores(number_of_cores); listener.Listen(); _exit(0); } @@ -58,7 +60,8 @@ void BecomeNaClLoader(const std::vector<int>& child_fds, // Some of this code was lifted from // content/browser/zygote_main_linux.cc:ForkWithRealPid() void HandleForkRequest(const std::vector<int>& child_fds, - size_t prereserved_sandbox_size) { + size_t prereserved_sandbox_size, + int number_of_cores) { VLOG(1) << "nacl_helper: forking"; pid_t childpid = fork(); if (childpid < 0) { @@ -96,7 +99,7 @@ void HandleForkRequest(const std::vector<int>& child_fds, if (HANDLE_EINTR(close(child_fds[kNaClParentFDIndex])) != 0) LOG(ERROR) << "close(child_fds[kNaClParentFDIndex]) failed"; if (validack) { - BecomeNaClLoader(child_fds, prereserved_sandbox_size); + BecomeNaClLoader(child_fds, prereserved_sandbox_size, number_of_cores); } else { LOG(ERROR) << "Failed to synch with zygote"; } @@ -236,6 +239,7 @@ int main(int argc, char* argv[]) { #endif std::vector<int> empty; // for SendMsg() calls size_t prereserved_sandbox_size = CheckReservedAtZero(); + int number_of_cores = sysconf(_SC_NPROCESSORS_ONLN); CheckRDebug(argv[0]); @@ -272,7 +276,7 @@ int main(int argc, char* argv[]) { } else if (msglen == sizeof(kNaClForkRequest) - 1 && memcmp(buf, kNaClForkRequest, msglen) == 0) { if (kNaClParentFDIndex + 1 == fds.size()) { - HandleForkRequest(fds, prereserved_sandbox_size); + HandleForkRequest(fds, prereserved_sandbox_size, number_of_cores); continue; // fork succeeded. Note: child does not return } else { LOG(ERROR) << "nacl_helper: unexpected number of fds, got " diff --git a/chrome/nacl/nacl_listener.cc b/chrome/nacl/nacl_listener.cc index 5f3ced4..477be0e 100644 --- a/chrome/nacl/nacl_listener.cc +++ b/chrome/nacl/nacl_listener.cc @@ -7,6 +7,10 @@ #include <errno.h> #include <stdlib.h> +#if defined(OS_POSIX) +#include <unistd.h> +#endif + #include "base/command_line.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" @@ -141,6 +145,9 @@ NaClListener::NaClListener() : shutdown_event_(true, false), #if defined(OS_LINUX) prereserved_sandbox_size_(0), #endif +#if defined(OS_POSIX) + number_of_cores_(-1), // unknown/error +#endif main_loop_(NULL) { io_thread_.StartWithOptions(base::Thread::Options(MessageLoop::TYPE_IO, 0)); #if defined(OS_WIN) @@ -226,6 +233,7 @@ void NaClListener::OnStart(const nacl::NaClStartParams& params) { LOG(ERROR) << "Failed to dup() the urandom FD"; return; } + args->number_of_cores = number_of_cores_; args->create_memory_object_func = CreateMemoryObject; # if defined(OS_MACOSX) CHECK(handles.size() >= 1); diff --git a/chrome/nacl/nacl_listener.h b/chrome/nacl/nacl_listener.h index 20e3e78..66627c0 100644 --- a/chrome/nacl/nacl_listener.h +++ b/chrome/nacl/nacl_listener.h @@ -34,6 +34,11 @@ class NaClListener : public IPC::Listener { prereserved_sandbox_size_ = prereserved_sandbox_size; } #endif +#if defined(OS_POSIX) + void set_number_of_cores(int number_of_cores) { + number_of_cores_ = number_of_cores; + } +#endif private: void OnStart(const nacl::NaClStartParams& params); @@ -51,6 +56,16 @@ class NaClListener : public IPC::Listener { #if defined(OS_LINUX) size_t prereserved_sandbox_size_; #endif +#if defined(OS_POSIX) + /* + * The outer sandbox on Linux and OSX prevents + * sysconf(_SC_NPROCESSORS) from working; in Windows, there are no + * problems with invoking GetSystemInfo. Therefore, only in + * OS_POSIX do we need to supply the number of cores into + * NaClChromeMainArgs object. + */ + int number_of_cores_; +#endif // Used to identify what thread we're on. MessageLoop* main_loop_; diff --git a/chrome/nacl/nacl_main.cc b/chrome/nacl/nacl_main.cc index 9b7a8c6..a7c1124 100644 --- a/chrome/nacl/nacl_main.cc +++ b/chrome/nacl/nacl_main.cc @@ -33,6 +33,12 @@ int NaClMain(const content::MainFunctionParams& parameters) { bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox); platform.InitSandboxTests(no_sandbox); +#if defined(OS_POSIX) + // The number of cores must be obtained before the invocation of + // platform.EnableSandbox(), so cannot simply be inlined below. + int number_of_cores = sysconf(_SC_NPROCESSORS_ONLN); +#endif + if (!no_sandbox) { platform.EnableSandbox(); } @@ -40,6 +46,9 @@ int NaClMain(const content::MainFunctionParams& parameters) { if (sandbox_test_result) { NaClListener listener; +#if defined(OS_POSIX) + listener.set_number_of_cores(number_of_cores); +#endif listener.Listen(); } else { // This indirectly prevents the test-harness-success-cookie from being set, diff --git a/chrome/test/data/nacl/nacl_test_data.gyp b/chrome/test/data/nacl/nacl_test_data.gyp index fc43fd1..54898d0 100644 --- a/chrome/test/data/nacl/nacl_test_data.gyp +++ b/chrome/test/data/nacl/nacl_test_data.gyp @@ -55,6 +55,22 @@ }, }, { + 'target_name': 'sysconf_nprocessors_onln_test', + 'type': 'none', + 'variables': { + 'nexe_target': 'sysconf_nprocessors_onln_test', + 'build_newlib': 1, + 'build_glibc': 1, + 'build_pnacl_newlib': 1, + 'sources': [ + 'sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc', + ], + 'test_files': [ + 'sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html', + ], + }, + }, + { 'target_name': 'ppapi_test_lib', 'type': 'none', 'variables': { diff --git a/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc b/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc new file mode 100644 index 0000000..86785ae9 --- /dev/null +++ b/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.cc @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2012 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* + * Post-message based test for simple rpc based access to sysconf result. + */ + +#include <assert.h> +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/fcntl.h> +#include <unistd.h> + +#include <sys/nacl_syscalls.h> + +#include <string> + +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/var.h" + +void NumProcessors(const pp::Var& message_data, std::string* sb) { + int num_cores; + char string_rep[16]; + + num_cores = sysconf(_SC_NPROCESSORS_ONLN); + snprintf(string_rep, sizeof string_rep, "%d", num_cores); + *sb = string_rep; +} + +struct PostMessageHandlerDesc { + char const *request; + void (*handler)(const pp::Var& message_data, std::string* out); +}; + +// This object represents one time the page says <embed>. +class MyInstance : public pp::Instance { + public: + explicit MyInstance(PP_Instance instance) : pp::Instance(instance) {} + virtual ~MyInstance() {} + virtual void HandleMessage(const pp::Var& message_data); +}; + +// HandleMessage gets invoked when postMessage is called on the DOM +// element associated with this plugin instance. In this case, if we +// are given a string, we'll post a message back to JavaScript with a +// reply string -- essentially treating this as a string-based RPC. +void MyInstance::HandleMessage(const pp::Var& message_data) { + static struct PostMessageHandlerDesc kMsgHandlers[] = { + { "nprocessors", NumProcessors }, + { reinterpret_cast<char const *>(NULL), + reinterpret_cast<void (*)(const pp::Var&, std::string*)>(NULL) } + }; + + if (message_data.is_string()) { + std::string op_name(message_data.AsString()); + size_t len; + + fprintf(stderr, "Searching for handler for request \"%s\".\n", + op_name.c_str()); + + std::string sb; + + for (size_t ix = 0; kMsgHandlers[ix].request != NULL; ++ix) { + if (op_name == kMsgHandlers[ix].request) { + fprintf(stderr, "found at index %u\n", ix); + kMsgHandlers[ix].handler(message_data, &sb); + break; + } + } + + len = strlen(sb.c_str()); + fprintf(stderr, "posting reply len %d\n", len); + fprintf(stderr, "posting reply \"%s\".\n", sb.c_str()); + fflush(stderr); + + PostMessage(pp::Var(sb)); + fprintf(stderr, "returning\n"); + fflush(stderr); + } +} + +// This object is the global object representing this plugin library as long +// as it is loaded. +class MyModule : public pp::Module { + public: + MyModule() : pp::Module() {} + virtual ~MyModule() {} + + // Override CreateInstance to create your customized Instance object. + virtual pp::Instance* CreateInstance(PP_Instance instance) { + return new MyInstance(instance); + } +}; + +namespace pp { + +// Factory function for your specialization of the Module object. +Module* CreateModule() { + return new MyModule(); +} + +} // namespace pp diff --git a/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html b/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html new file mode 100644 index 0000000..335420c --- /dev/null +++ b/chrome/test/data/nacl/sysconf_nprocessors_onln/sysconf_nprocessors_onln_test.html @@ -0,0 +1,59 @@ +<!-- + Copyright (c) 2011 The Chromium Authors. All rights reserved. + Use of this source code is governed by a BSD-style license that can + be found in the LICENSE file. +--> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + <head> + <meta http-equiv="Pragma" content="no-cache" /> + <meta http-equiv="Expires" content="-1" /> + <script type="text/javascript" src="nacltest.js"></script> + <title>Native Client Sysconf _SC_NPROCESSORS_ONLN Test</title> + </head> + + <body id="body"> + <h1>Native Client _SC_NPROCESSORS_ONLN Test</h1> + <script type="text/javascript"> + //<![CDATA[ +var tester = new Tester($('body')); +var gotExpected = false; +var args = getTestArguments({'cpu_count' : + 'THIS TEST CANNOT RUN STANDALONE -- run using browser_test instead'}); + +function setupTests(tester, plugin) { + tester.addAsyncTest('TestSysconfNprocessors', function(status) { + plugin.addEventListener('message', function handler(message_event) { + this.removeEventListener('message', handler, false); + status.assertEqual(message_event.data, args.cpu_count); + status.pass(); + }, false); + plugin.postMessage('nprocessors'); + }); +} + +function runTests() { + setupTests(tester, $('naclModule')); + tester.waitFor($('naclModule')); + tester.run(); +} + //]]> + </script> + <div> + <embed id="naclModule" + name="naclModule" + width=400 height=400 + src="sysconf_nprocessors_onln_test.nmf" + basic_tests="2" + stress_tests="0" + style="background-color:gray" + type="application/x-nacl" /> + </div> + <script type="text/javascript"> + //<![CDATA[ +runTests(); + //]]> + </script> + </body> +</html> diff --git a/chrome/test/nacl/nacl_browsertest.cc b/chrome/test/nacl/nacl_browsertest.cc index 9336311..3e34870 100644 --- a/chrome/test/nacl/nacl_browsertest.cc +++ b/chrome/test/nacl/nacl_browsertest.cc @@ -2,6 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <stdio.h> +#if defined(OS_POSIX) +#include <unistd.h> +#elif defined(OS_WIN) +#include <windows.h> +#endif + #include "chrome/test/nacl/nacl_browsertest_util.h" namespace { @@ -18,6 +25,7 @@ namespace { #define MAYBE_CrossOriginFail DISABLED_CrossOriginFail #define MAYBE_SameOriginCookie DISABLED_SameOriginCookie #define MAYBE_CORSNoCookie DISABLED_CORSNoCookie +#define MAYBE_SysconfNprocessorsOnln DISABLED_SysconfNprocessorsOnln #else #define MAYBE_SimpleLoad SimpleLoad #define MAYBE_ExitStatus0 ExitStatus0 @@ -29,6 +37,7 @@ namespace { #define MAYBE_CrossOriginFail CrossOriginFail #define MAYBE_SameOriginCookie SameOriginCookie #define MAYBE_CORSNoCookie CORSNoCookie +#define MAYBE_SysconfNprocessorsOnln SysconfNprocessorsOnln #endif NACL_BROWSER_TEST_F(NaClBrowserTest, MAYBE_SimpleLoad, { @@ -58,6 +67,36 @@ NACL_BROWSER_TEST_F(NaClBrowserTest, MAYBE_ProgressEvents, { RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_progress_events.html")); }) +// Visual Studio does not like preprocessor conditionals inside the +// argument of a macro, so we put the conditionals on a helper +// function. We are already in an anonymous namespace, so the name of +// the helper is not visible in external scope. +// +// The string buffer is sufficient sized: 2**64 < 8**22 < 10**22. +#if defined(OS_POSIX) +base::FilePath::StringType NumberOfCoresAsFilePathString() { + char string_rep[23]; + snprintf(string_rep, sizeof string_rep, "%ld", sysconf(_SC_NPROCESSORS_ONLN)); + return string_rep; +} +#elif defined(OS_WIN) +base::FilePath::StringType NumberOfCoresAsFilePathString() { + wchar_t string_rep[23]; + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + _snwprintf_s(string_rep, sizeof string_rep, _TRUNCATE, L"%u", + system_info.dwNumberOfProcessors); + return string_rep; +} +#endif + +NACL_BROWSER_TEST_F(NaClBrowserTest, MAYBE_SysconfNprocessorsOnln, { + base::FilePath::StringType path = + FILE_PATH_LITERAL("sysconf_nprocessors_onln_test.html?cpu_count="); + path = path + NumberOfCoresAsFilePathString(); + RunNaClIntegrationTest(path); +}) + IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, MAYBE_CrossOriginCORS) { RunLoadTest(FILE_PATH_LITERAL("cross_origin/cors.html")); } |