1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraTraces"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include "utils/CameraTraces.h"
22#include <utils/ProcessCallStack.h>
23
24#include <utils/Mutex.h>
25#include <utils/List.h>
26
27#include <utils/Log.h>
28#include <cutils/trace.h>
29
30namespace android {
31namespace camera3 {
32
33struct CameraTracesImpl {
34    Mutex                    tracesLock;
35    List<ProcessCallStack>   pcsList;
36}; // class CameraTraces::Impl;
37
38static CameraTracesImpl gImpl;
39CameraTracesImpl& CameraTraces::sImpl = gImpl;
40
41void CameraTraces::saveTrace() {
42    ALOGV("%s: begin", __FUNCTION__);
43    ATRACE_BEGIN("CameraTraces::saveTrace");
44    Mutex::Autolock al(sImpl.tracesLock);
45
46    List<ProcessCallStack>& pcsList = sImpl.pcsList;
47
48    // Insert new ProcessCallStack, and immediately crawl all the threads
49    pcsList.push_front(ProcessCallStack());
50    ProcessCallStack& pcs = *pcsList.begin();
51    pcs.update();
52
53    if (pcsList.size() > MAX_TRACES) {
54        // Prune list periodically and discard oldest entry
55        pcsList.erase(--pcsList.end());
56    }
57
58    IF_ALOGV() {
59        pcs.log(LOG_TAG, ANDROID_LOG_VERBOSE);
60    }
61
62    ALOGD("Process trace saved. Use dumpsys media.camera to view.");
63
64    ATRACE_END();
65}
66
67status_t CameraTraces::dump(int fd, const Vector<String16> &args __attribute__((unused))) {
68    ALOGV("%s: fd = %d", __FUNCTION__, fd);
69    Mutex::Autolock al(sImpl.tracesLock);
70    List<ProcessCallStack>& pcsList = sImpl.pcsList;
71
72    if (fd < 0) {
73        ALOGW("%s: Negative FD (%d)", __FUNCTION__, fd);
74        return BAD_VALUE;
75    }
76
77    dprintf(fd, "Camera traces (%zu):\n", pcsList.size());
78
79    if (pcsList.empty()) {
80        dprintf(fd, "  No camera traces collected.\n");
81    }
82
83    // Print newest items first
84    List<ProcessCallStack>::iterator it, end;
85    for (it = pcsList.begin(), end = pcsList.end(); it != end; ++it) {
86        const ProcessCallStack& pcs = *it;
87        pcs.dump(fd, DUMP_INDENT);
88    }
89
90    return OK;
91}
92
93}; // namespace camera3
94}; // namespace android
95