Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pids #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions utils/arcvm-time-sync-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/gradle.xml
/.idea/misc.xml
/.idea/compiler.xml
/.idea/vcs.xml
/.idea/.name
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions utils/arcvm-time-sync-app/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/arcvm-time-sync-app/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
51 changes: 51 additions & 0 deletions utils/arcvm-time-sync-app/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id 'com.android.application'
}

android {
compileSdk 30

defaultConfig {
applicationId "com.google.perfettoguesttimesync"
minSdk 28
targetSdk 30
versionCode 4
versionName "1.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ''
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions utils/arcvm-time-sync-app/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Binary file not shown.
20 changes: 20 additions & 0 deletions utils/arcvm-time-sync-app/app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.google.perfettoguesttimesync",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 4,
"versionName": "1.3",
"outputFile": "app-release.apk"
}
],
"elementType": "File"
}
20 changes: 20 additions & 0 deletions utils/arcvm-time-sync-app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.perfettoguesttimesync">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PerfettoGuestTimeSync">
<service
android:name=".TimeTrace"
android:exported="true">
</service>
</application>

</manifest>
51 changes: 51 additions & 0 deletions utils/arcvm-time-sync-app/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("perfettoguesttimesync")

find_package(Threads)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
perfettoguesttimesync

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
native-lib.cpp
perfetto.cc)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
perfettoguesttimesync

# Links the target library to the log library
# included in the NDK.
${log-lib})
108 changes: 108 additions & 0 deletions utils/arcvm-time-sync-app/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2021 The Android Open Source Project
//
// 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 <jni.h>
#include <string>
#include <thread>
#include <sys/types.h>
#include <time.h>

#if defined(_M_IA64) || defined(_M_IX86) || defined(__ia64__) || \
defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
defined(_M_AMD64)
#define HAS_RDTSC
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#endif
#if defined(__aarch64__)
#define HAS_CNTCVT
#endif

#include "perfetto.h"

PERFETTO_DEFINE_CATEGORIES(
perfetto::Category("cros")
.SetDescription("Chrome OS guest time sync events"));

PERFETTO_TRACK_EVENT_STATIC_STORAGE();

static std::once_flag perfetto_once;
static std::thread* bg_thread = nullptr;

static inline uint64_t get_cpu_ticks() {
#if defined(HAS_RDTSC)
return __rdtsc();
#elif defined(HAS_CNTCVT)
uint64_t vct;
asm volatile("mrs %0, cntvct_el0" : "=r"(vct));
return vct;
#else
return 0;
#endif
}

static inline uint64_t get_timestamp_ns(clockid_t cid) {
struct timespec ts = {};
clock_gettime(cid, &ts);
return static_cast<uint64_t>(ts.tv_sec * 1000000000LL + ts.tv_nsec);
}

void perfetto_annotate_time_sync(const perfetto::EventContext& perfetto) {
uint64_t boot_time = get_timestamp_ns(CLOCK_BOOTTIME);
uint64_t cpu_time = get_cpu_ticks();
uint64_t monotonic_time = get_timestamp_ns(CLOCK_MONOTONIC);
// Read again to avoid cache miss overhead.
boot_time = get_timestamp_ns(CLOCK_BOOTTIME);
cpu_time = get_cpu_ticks();
monotonic_time = get_timestamp_ns(CLOCK_MONOTONIC);

auto* dbg = perfetto.event()->add_debug_annotations();
dbg->set_name("clock_sync_boottime");
dbg->set_uint_value(boot_time);
dbg = perfetto.event()->add_debug_annotations();
dbg->set_name("clock_sync_monotonic");
dbg->set_uint_value(monotonic_time);
dbg = perfetto.event()->add_debug_annotations();
dbg->set_name("clock_sync_cputime");
dbg->set_uint_value(cpu_time);
}

void tick_forever() {
for(;;) {
usleep(100000);
TRACE_EVENT(
"cros", "guest_clock_sync",
[&](perfetto::EventContext p) { perfetto_annotate_time_sync(p); });
}
}

void init_perfetto()
{
std::call_once(perfetto_once, [](){
perfetto::TracingInitArgs args;
args.backends |= perfetto::kSystemBackend;
perfetto::Tracing::Initialize(args);
perfetto::TrackEvent::Register();
bg_thread = new std::thread(tick_forever);
});
}

extern "C" JNIEXPORT void JNICALL
Java_com_google_perfettoguesttimesync_TimeTrace_perfettoInit(
JNIEnv* env,
jobject /* this */) {
init_perfetto();
}
Loading