Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: content/common/android/linker/linker_jni.cc

Issue 59033008: android: Make org.chromium.base.SysUtils.isLowEndDevice() work without native code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatting Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is the Android-specific content linker, a tiny shared library 5 // This is the Android-specific content linker, a tiny shared library
6 // implementing a custom dynamic linker that can be used to load the 6 // implementing a custom dynamic linker that can be used to load the
7 // real content-based libraries (e.g. libcontentshell.so). 7 // real content-based libraries (e.g. libcontentshell.so).
8 8
9 // The main point of this linker is to be able to share the RELRO 9 // The main point of this linker is to be able to share the RELRO
10 // section of libcontentshell.so (or equivalent) between the browser and 10 // section of libcontentshell.so (or equivalent) between the browser and
11 // renderer process. 11 // renderer process.
12 12
13 // This source code *cannot* depend on anything from base/ or the C++ 13 // This source code *cannot* depend on anything from base/ or the C++
14 // STL, to keep the final library small, and avoid ugly dependency issues. 14 // STL, to keep the final library small, and avoid ugly dependency issues.
15 15
16 #include <android/log.h> 16 #include <android/log.h>
17 #include <crazy_linker.h> 17 #include <crazy_linker.h>
18 #include <jni.h> 18 #include <jni.h>
19 #include <stdlib.h> 19 #include <stdlib.h>
20 #include <unistd.h> 20 #include <unistd.h>
21 21
22 // Any device that reports a physical RAM size less than this, in megabytes
23 // is considered 'low-end'. IMPORTANT: Read the LinkerLowMemoryThresholdTest
24 // comments in build/android/pylib/linker/test_case.py before modifying this
25 // value.
26 #define ANDROID_LOW_MEMORY_DEVICE_THRESHOLD_MB 512
27
28 // Set this to 1 to enable debug traces to the Android log. 22 // Set this to 1 to enable debug traces to the Android log.
29 // Note that LOG() from "base/logging.h" cannot be used, since it is 23 // Note that LOG() from "base/logging.h" cannot be used, since it is
30 // in base/ which hasn't been loaded yet. 24 // in base/ which hasn't been loaded yet.
31 #define DEBUG 0 25 #define DEBUG 0
32 26
33 #define TAG "content_android_linker" 27 #define TAG "content_android_linker"
34 28
35 #if DEBUG 29 #if DEBUG
36 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) 30 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
37 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) 31 #define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { 375 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) {
382 return crazy_system_can_share_relro(); 376 return crazy_system_can_share_relro();
383 } 377 }
384 378
385 jlong GetPageSize(JNIEnv* env, jclass clazz) { 379 jlong GetPageSize(JNIEnv* env, jclass clazz) {
386 jlong result = static_cast<jlong>(sysconf(_SC_PAGESIZE)); 380 jlong result = static_cast<jlong>(sysconf(_SC_PAGESIZE));
387 LOG_INFO("%s: System page size is %lld bytes\n", __FUNCTION__, result); 381 LOG_INFO("%s: System page size is %lld bytes\n", __FUNCTION__, result);
388 return result; 382 return result;
389 } 383 }
390 384
391 jboolean IsLowMemoryDevice(JNIEnv* env, jclass clazz) {
392 // This matches the implementation of org.chromium.base.SysUtils.isLowEnd(),
393 // however this Java method relies on native code from base/, which isn't
394 // available since the library hasn't been loaded yet.
395 // The value ANDROID_LOW_MEMORY_DEVICE_THRESHOLD_MB is the same for both
396 // sources.
397
398 // Threshold for low-end memory devices.
399 const size_t kMegaBytes = 1024 * 1024;
400 const size_t kLowMemoryDeviceThreshold =
401 ANDROID_LOW_MEMORY_DEVICE_THRESHOLD_MB * kMegaBytes;
402
403 // Compute the amount of physical RAM on the device.
404 size_t pages = static_cast<size_t>(sysconf(_SC_PHYS_PAGES));
405 size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
406 size_t physical_size = pages * page_size;
407
408 LOG_INFO("%s: System physical size is %zu MB\n",
409 __FUNCTION__,
410 physical_size / kMegaBytes);
411
412 return physical_size <= kLowMemoryDeviceThreshold;
413 }
414
415 const JNINativeMethod kNativeMethods[] = { 385 const JNINativeMethod kNativeMethods[] = {
416 {"nativeLoadLibrary", 386 {"nativeLoadLibrary",
417 "(" 387 "("
418 "Ljava/lang/String;" 388 "Ljava/lang/String;"
419 "J" 389 "J"
420 "Lorg/chromium/content/app/Linker$LibInfo;" 390 "Lorg/chromium/content/app/Linker$LibInfo;"
421 ")" 391 ")"
422 "Z", 392 "Z",
423 reinterpret_cast<void*>(&LoadLibrary)}, 393 reinterpret_cast<void*>(&LoadLibrary)},
424 {"nativeCreateSharedRelro", 394 {"nativeCreateSharedRelro",
(...skipping 13 matching lines...) Expand all
438 reinterpret_cast<void*>(&UseSharedRelro)}, 408 reinterpret_cast<void*>(&UseSharedRelro)},
439 {"nativeCanUseSharedRelro", 409 {"nativeCanUseSharedRelro",
440 "(" 410 "("
441 ")" 411 ")"
442 "Z", 412 "Z",
443 reinterpret_cast<void*>(&CanUseSharedRelro)}, 413 reinterpret_cast<void*>(&CanUseSharedRelro)},
444 {"nativeGetPageSize", 414 {"nativeGetPageSize",
445 "(" 415 "("
446 ")" 416 ")"
447 "J", 417 "J",
448 reinterpret_cast<void*>(&GetPageSize)}, 418 reinterpret_cast<void*>(&GetPageSize)}, };
449 {"nativeIsLowMemoryDevice",
450 "("
451 ")"
452 "Z",
453 reinterpret_cast<void*>(&IsLowMemoryDevice)}, };
454 419
455 } // namespace 420 } // namespace
456 421
457 // JNI_OnLoad() hook called when the linker library is loaded through 422 // JNI_OnLoad() hook called when the linker library is loaded through
458 // the regular System.LoadLibrary) API. This shall save the Java VM 423 // the regular System.LoadLibrary) API. This shall save the Java VM
459 // handle and initialize LibInfo fields. 424 // handle and initialize LibInfo fields.
460 jint JNI_OnLoad(JavaVM* vm, void* reserved) { 425 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
461 LOG_INFO("%s: Entering", __FUNCTION__); 426 LOG_INFO("%s: Entering", __FUNCTION__);
462 // Get new JNIEnv 427 // Get new JNIEnv
463 JNIEnv* env; 428 JNIEnv* env;
(...skipping 19 matching lines...) Expand all
483 return -1; 448 return -1;
484 } 449 }
485 450
486 // Save JavaVM* handle into context. 451 // Save JavaVM* handle into context.
487 crazy_context_t* context = GetCrazyContext(); 452 crazy_context_t* context = GetCrazyContext();
488 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); 453 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4);
489 454
490 LOG_INFO("%s: Done", __FUNCTION__); 455 LOG_INFO("%s: Done", __FUNCTION__);
491 return JNI_VERSION_1_4; 456 return JNI_VERSION_1_4;
492 } 457 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698