नेटिव कोड में कस्टम ट्रेस इवेंट
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
Android 6.0 (एपीआई लेवल 23) और उसके बाद के वर्शन में, नेटिव ट्रेसिंग एपीआई trace.h
,
का इस्तेमाल करके सिस्टम बफ़र में ट्रेस इवेंट लिखें. बाद में इनका इस्तेमाल करके विश्लेषण किया जा सकता है
परफ़ेटो या सिस्ट्रेस. इस एपीआई के सामान्य इस्तेमाल के उदाहरणों में, समय के बारे में जानकारी शामिल है
जिसे कोड के एक ब्लॉक को एक्ज़ीक्यूट करने और जोड़ने के लिए इस्तेमाल किया जाता है
असामान्य तरीके से काम नहीं करता है.
ध्यान दें: एपीआई लेवल 27 और उससे पहले के वर्शन वाले डिवाइसों और एम्युलेटर पर, अगर वहां
पर्याप्त मेमोरी उपलब्ध नहीं है या मेमोरी बहुत फ़्रैगमेंट की गई है, तो आपको
नीचे दिया गया मैसेज: Atrace could not allocate enough memory to record a trace
.
अगर ऐसा होता है और आपके कैप्चर किए गए डेटा में डेटा का पूरा सेट नहीं है, तो
को बैकग्राउंड की प्रोसेस बंद करनी चाहिए या डिवाइस या एम्युलेटर को रीस्टार्ट करना चाहिए.
अपने ऐप्लिकेशन या गेम में नेटिव कोड में होने वाले कस्टम इवेंट तय करने के लिए,
इन चरणों को पूरा करें:
जिन ATrace फ़ंक्शन का इस्तेमाल किया जाता है उनके लिए फ़ंक्शन पॉइंटर तय करना
अपने ऐप्लिकेशन या गेम में कस्टम इवेंट कैप्चर करें, जैसा कि इस कोड में दिखाया गया है
snippet:
#include <android/trace.h>
#include <dlfcn.h>
void *(*ATrace_beginSection) (const char* sectionName);
void *(*ATrace_endSection) (void);
typedef void *(*fp_ATrace_beginSection) (const char* sectionName);
typedef void *(*fp_ATrace_endSection) (void);
इस कोड में दिखाए गए तरीके से, रनटाइम के दौरान एट्रेस सिंबल लोड करें
स्निपेट. आम तौर पर, यह प्रोसेस किसी ऑब्जेक्ट कंस्ट्रक्टर में की जाती है.
// Retrieve a handle to libandroid.
void *lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
// Access the native tracing functions.
if (lib != NULL) {
// Use dlsym() to prevent crashes on devices running Android 5.1
// (API level 22) or lower.
ATrace_beginSection = reinterpret_cast<fp_ATrace_beginSection>(
dlsym(lib, "ATrace_beginSection"));
ATrace_endSection = reinterpret_cast<fp_ATrace_endSection>(
dlsym(lib, "ATrace_endSection"));
}
चेतावनी: सुरक्षा वजहों से,
dlopen()
की वैल्यू सिर्फ़ आपके ऐप्लिकेशन या गेम के डीबग वर्शन में उपलब्ध है.
ध्यान दें: आने वाले समय में, ट्रेस करने की सुविधा उपलब्ध कराई जा सके
Android 4.3 (एपीआई लेवल 18) के साथ काम करता है, तो JNI का इस्तेमाल करके
मैनेज किया जा रहा कोड
पिछला स्निपेट.
ATrace_beginSection()
पर कॉल करें और
ATrace_endSection()
इसके पहले और आखिर में,
आपका कस्टम इवेंट:
#include <android/trace.h>
char *customEventName = new char[32];
sprintf(customEventName, "User tapped %s button", buttonName);
ATrace_beginSection(customEventName);
// Your app or game's response to the button being pressed.
ATrace_endSection();
ध्यान दें: जब आप ATrace_beginSection()
को एक से ज़्यादा कॉल करते हैं
बार-बार, ATrace_endSection()
को कॉल करने की सुविधा बहुत ज़्यादा बार बंद होती है
हाल ही में ATrace_beginSection()
तरीका कहा गया. इसलिए, नेस्ट किए गए
कॉल करते हैं, तो सुनिश्चित करें कि आप
इनको कॉल करने वाला ATrace_beginSection()
ATrace_endSection()
.
इसके अलावा, आप ATrace_beginSection()
को एक नंबर पर कॉल नहीं कर सकते
थ्रेड और इसे किसी दूसरी थ्रेड से खत्म करें. आपको दोनों फ़ंक्शन को एक ही से कॉल करना होगा
थ्रेड.
सुविधा के बारे में सलाह
नीचे दी गई सलाह ज़रूरी नहीं है. हालांकि, इनसे आपकी साइट के पेजों का विश्लेषण करना आसान हो सकता है
कोड.
पूरे फ़ंक्शन को ट्रेस करना
कॉल स्टैक या फ़ंक्शन का समय इंस्ट्रुमेंट करते समय, यह आपको काम का लग सकता है
का इस्तेमाल करके, पूरे फ़ंक्शन को ट्रेस किया जा सकता है. इसे बनाने के लिए, ATRACE_CALL()
मैक्रो का इस्तेमाल किया जा सकता है
ट्रेस करने की प्रोसेस को आसानी से सेट अप किया जा सकता है. इसके अलावा, ऐसे मैक्रो से आपको दो अलग-अलग तरीकों से तेज़ी से
उन मामलों के लिए try
और catch
ब्लॉक बनाया जा रहा है जहां ट्रेस किया गया फ़ंक्शन शायद
कोई अपवाद दें या return
को पहले कॉल करें.
पूरे फ़ंक्शन को ट्रेस करने के लिए मैक्रो बनाने के लिए, नीचे दिए गए चरणों को पूरा करें:
मैक्रो निर्धारित करें:
#define ATRACE_NAME(name) ScopedTrace ___tracer(name)
// ATRACE_CALL is an ATRACE_NAME that uses the current function name.
#define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)
class ScopedTrace {
public:
inline ScopedTrace(const char *name) {
ATrace_beginSection(name);
}
inline ~ScopedTrace() {
ATrace_endSection();
}
};
उस फ़ंक्शन में मैक्रो को कॉल करें जिसे आप ट्रेस करना चाहते हैं:
void myExpensiveFunction() {
ATRACE_CALL();
// Code that you want to trace.
}
अपने थ्रेड को नाम दें
जैसा कि दिखाया गया है, आप हर उस थ्रेड को नाम दे सकते हैं जिसमें आपके इवेंट होते हैं
को जोड़ना होगा. इस चरण से थ्रेड की पहचान करना आसान हो जाता है
जो आपके गेम में हुई खास कार्रवाइयों से जुड़े हैं.
#include <pthread.h>
static void *render_scene(void *parm) {
// Code for preparing your app or game's visual components.
}
static void *load_main_menu(void *parm) {
// Code that executes your app or game's main logic.
}
void init_threads() {
pthread_t render_thread, main_thread;
pthread_create(&render_thread, NULL, render_scene, NULL);
pthread_create(&main_thread, NULL, load_main_menu, NULL);
pthread_setname_np(render_thread, "MyRenderer");
pthread_setname_np(main_thread, "MyMainMenu");
}
आपके लिए सुझाव
इस पेज पर मौजूद कॉन्टेंट और कोड सैंपल कॉन्टेंट के लाइसेंस में बताए गए लाइसेंस के हिसाब से हैं. Java और OpenJDK, Oracle और/या इससे जुड़ी हुई कंपनियों के ट्रेडमार्क या रजिस्टर किए हुए ट्रेडमार्क हैं.
आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया.
[[["समझने में आसान है","easyToUnderstand","thumb-up"],["मेरी समस्या हल हो गई","solvedMyProblem","thumb-up"],["अन्य","otherUp","thumb-up"]],[["वह जानकारी मौजूद नहीं है जो मुझे चाहिए","missingTheInformationINeed","thumb-down"],["बहुत मुश्किल है / बहुत सारे चरण हैं","tooComplicatedTooManySteps","thumb-down"],["पुराना","outOfDate","thumb-down"],["अनुवाद से जुड़ी समस्या","translationIssue","thumb-down"],["सैंपल / कोड से जुड़ी समस्या","samplesCodeIssue","thumb-down"],["अन्य","otherDown","thumb-down"]],["आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया."],[],[],null,["# Custom trace events in native code\n\nAndroid 6.0 (API level 23) and higher support a native tracing API, `trace.h`,\nto write trace events to the system buffer that you can then analyze using\nPerfetto or systrace. Common use cases for this API include observing the time\nthat a particular block of code takes to execute and associating a block of code\nwith undesirable system behavior.\n\n**Note:** On devices and emulators running API level 27 and lower, if there\nisn't enough memory available or the memory is too fragmented, you'll get the\nfollowing message: `Atrace could not allocate enough memory to record a trace`.\nIf this happens and your capture does not have a complete set of data, then you\nshould close background processes or restart the device or emulator.\n\nTo define custom events that occur in the native code within your app or game,\ncomplete the following steps:\n\n1. Define function pointers for the ATrace functions that you use to\n capture custom events within your app or game, as shown in the following code\n snippet:\n\n ```c++\n #include \u003candroid/trace.h\u003e\n #include \u003cdlfcn.h\u003e\n\n void *(*ATrace_beginSection) (const char* sectionName);\n void *(*ATrace_endSection) (void);\n\n typedef void *(*fp_ATrace_beginSection) (const char* sectionName);\n typedef void *(*fp_ATrace_endSection) (void);\n ```\n2. Load the ATrace symbols at runtime, as shown in the following code\n snippet. Usually, you perform this process in an object constructor.\n\n ```c++\n // Retrieve a handle to libandroid.\n void *lib = dlopen(\"libandroid.so\", RTLD_NOW | RTLD_LOCAL);\n\n // Access the native tracing functions.\n if (lib != NULL) {\n // Use dlsym() to prevent crashes on devices running Android 5.1\n // (API level 22) or lower.\n ATrace_beginSection = reinterpret_cast\u003cfp_ATrace_beginSection\u003e(\n dlsym(lib, \"ATrace_beginSection\"));\n ATrace_endSection = reinterpret_cast\u003cfp_ATrace_endSection\u003e(\n dlsym(lib, \"ATrace_endSection\"));\n }\n ```\n\n **Caution:** For security reasons, include calls to\n `dlopen()` only in the debug version of your app or game.\n\n **Note:** To provide tracing support further back to\n Android 4.3 (API level 18), you can use JNI to call the methods in\n [managed code](#managed-code) around the code shown in the\n preceding snippet.\n3. Call `ATrace_beginSection()` and\n `ATrace_endSection()` at the beginning and end, respectively, of\n your custom event:\n\n ```c++\n #include \u003candroid/trace.h\u003e\n\n char *customEventName = new char[32];\n sprintf(customEventName, \"User tapped %s button\", buttonName);\n\n ATrace_beginSection(customEventName);\n // Your app or game's response to the button being pressed.\n ATrace_endSection();\n ``` \n **Note:** When you call `ATrace_beginSection()` multiple\n times, calling `ATrace_endSection()` ends only the most\n recently called `ATrace_beginSection()` method. So, for nested\n calls, make sure that you properly match each call to\n `ATrace_beginSection()` with a call to\n `ATrace_endSection()`.\n\n Additionally, you cannot call `ATrace_beginSection()` on one\n thread and end it from another. You must call both functions from the same\n thread.\n\nConvenience tips\n================\n\nThe following tips are optional but might make it easier to analyze your native\ncode.\n\nTrace an entire function\n------------------------\n\nWhen instrumenting your call stack or function timing, you might find it useful\nto trace entire functions. You can use the `ATRACE_CALL()` macro to make this\ntype of tracing easier to set up. Furthermore, such a macro allows you to skip\ncreating `try` and `catch` blocks for cases where the traced function might\nthrow an exception or call `return` early.\n\nTo create a macro for tracing an entire function, complete the following steps:\n\n1. Define the macro:\n\n ```c++\n #define ATRACE_NAME(name) ScopedTrace ___tracer(name)\n\n // ATRACE_CALL is an ATRACE_NAME that uses the current function name.\n #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)\n\n class ScopedTrace {\n public:\n inline ScopedTrace(const char *name) {\n ATrace_beginSection(name);\n }\n\n inline ~ScopedTrace() {\n ATrace_endSection();\n }\n };\n ```\n2. Call the macro within the function that you want to trace:\n\n ```c++\n void myExpensiveFunction() {\n ATRACE_CALL();\n // Code that you want to trace.\n }\n ```\n\nName your threads\n-----------------\n\nYou can give a name to each thread in which your events occur, as demonstrated\nin the following code snippet. This step makes it easier to identify the threads\nthat belong to specific actions within your game. \n\n```c++\n#include \u003cpthread.h\u003e\n\nstatic void *render_scene(void *parm) {\n // Code for preparing your app or game's visual components.\n}\n\nstatic void *load_main_menu(void *parm) {\n // Code that executes your app or game's main logic.\n}\n\nvoid init_threads() {\n pthread_t render_thread, main_thread;\n\n pthread_create(&render_thread, NULL, render_scene, NULL);\n pthread_create(&main_thread, NULL, load_main_menu, NULL);\n\n pthread_setname_np(render_thread, \"MyRenderer\");\n pthread_setname_np(main_thread, \"MyMainMenu\");\n}\n```\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Best practices for SQLite performance](/topic/performance/sqlite-performance-best-practices)\n- [Create and measure Baseline Profiles without Macrobenchmark](/topic/performance/baselineprofiles/manually-create-measure)"]]