Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b9490783 authored by Rhed Jao's avatar Rhed Jao Committed by Android (Google) Code Review
Browse files

Merge changes I85abef8a,I107b3de9,I1cb593d2,Iea3a6e67

* changes:
  Faster bugreports (6/n)
  Faster bugreports (5/n)
  Faster bugreports (4/n)
  Faster bugreports (3/n)
parents e4337f1b b5685b33
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ cc_binary {
    defaults: ["dumpstate_defaults"],
    srcs: [
        "DumpPool.cpp",
        "TaskQueue.cpp",
        "dumpstate.cpp",
        "main.cpp",
    ],
@@ -134,6 +135,7 @@ cc_test {
    defaults: ["dumpstate_defaults"],
    srcs: [
        "DumpPool.cpp",
        "TaskQueue.cpp",
        "dumpstate.cpp",
        "tests/dumpstate_test.cpp",
    ],
@@ -151,6 +153,7 @@ cc_test {
    defaults: ["dumpstate_defaults"],
    srcs: [
        "DumpPool.cpp",
        "TaskQueue.cpp",
        "dumpstate.cpp",
        "tests/dumpstate_smoke_test.cpp",
    ],
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 "TaskQueue.h"

namespace android {
namespace os {
namespace dumpstate {

TaskQueue::~TaskQueue() {
    run(/* do_cancel = */true);
}

void TaskQueue::run(bool do_cancel) {
    std::unique_lock lock(lock_);
    while (!tasks_.empty()) {
        auto task = tasks_.front();
        tasks_.pop();
        lock.unlock();
        std::invoke(task, do_cancel);
        lock.lock();
    }
}

}  // namespace dumpstate
}  // namespace os
}  // namespace android
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.
 */

#ifndef FRAMEWORK_NATIVE_CMD_TASKQUEUE_H_
#define FRAMEWORK_NATIVE_CMD_TASKQUEUE_H_

#include <mutex>
#include <queue>

#include <android-base/macros.h>

namespace android {
namespace os {
namespace dumpstate {

/*
 * A task queue for dumpstate to collect tasks such as adding file to the zip
 * which are needed to run in a single thread. The task is a callable function
 * included a cancel task boolean parameter. The TaskQueue could
 * cancel the task in the destructor if the task has never been called.
 */
class TaskQueue {
  public:
    TaskQueue() = default;
    ~TaskQueue();

    /*
     * Adds a task into the queue.
     *
     * |f| Callable function to execute the task. The function must include a
     *     boolean parameter for TaskQueue to notify whether the task is
     *     cancelled or not.
     * |args| A list of arguments.
     */
    template<class F, class... Args> void add(F&& f, Args&&... args) {
        auto func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
        std::unique_lock lock(lock_);
        tasks_.emplace([=](bool cancelled) {
            std::invoke(func, cancelled);
        });
    }

    /*
     * Invokes all tasks in the task queue.
     *
     * |do_cancel| true to cancel all tasks in the queue.
     */
    void run(bool do_cancel);

  private:
    using Task = std::function<void(bool)>;

    std::mutex lock_;
    std::queue<Task> tasks_;

    DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};

}  // namespace dumpstate
}  // namespace os
}  // namespace android

#endif //FRAMEWORK_NATIVE_CMD_TASKQUEUE_H_
+242 −94

File changed.

Preview size limit exceeded, changes collapsed.

+31 −3
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@

#include "DumpstateUtil.h"
#include "DumpPool.h"
#include "TaskQueue.h"

// Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
// std::vector<std::string>
@@ -229,11 +230,13 @@ class Dumpstate {
     * |full_command| array containing the command (first entry) and its arguments.
     * Must contain at least one element.
     * |options| optional argument defining the command's behavior.
     * |out_fd| A fd to support the DumpPool to output results to a temporary
     * file. Using STDOUT_FILENO if it's not running in the parallel task.
     */
    int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
                   const android::os::dumpstate::CommandOptions& options =
                       android::os::dumpstate::CommandOptions::DEFAULT,
                   bool verbose_duration = false);
                   bool verbose_duration = false, int out_fd = STDOUT_FILENO);

    /*
     * Runs `dumpsys` with the given arguments, automatically setting its timeout
@@ -246,10 +249,12 @@ class Dumpstate {
     * |options| optional argument defining the command's behavior.
     * |dumpsys_timeout| when > 0, defines the value passed to `dumpsys -T` (otherwise it uses the
     * timeout from `options`)
     * |out_fd| A fd to support the DumpPool to output results to a temporary
     * file. Using STDOUT_FILENO if it's not running in the parallel task.
     */
    void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
                    const android::os::dumpstate::CommandOptions& options = DEFAULT_DUMPSYS,
                    long dumpsys_timeout_ms = 0);
                    long dumpsys_timeout_ms = 0, int out_fd = STDOUT_FILENO);

    /*
     * Prints the contents of a file.
@@ -306,7 +311,12 @@ class Dumpstate {
    // Returns OK in all other cases.
    RunStatus DumpTraces(const char** path);

    void DumpstateBoard();
    /*
     * |out_fd| A fd to support the DumpPool to output results to a temporary file.
     * Dumpstate can pick up later and output to the bugreport. Using STDOUT_FILENO
     * if it's not running in the parallel task.
     */
    void DumpstateBoard(int out_fd = STDOUT_FILENO);

    /*
     * Updates the overall progress of the bugreport generation by the given weight increment.
@@ -362,6 +372,18 @@ class Dumpstate {
     */
    bool CalledByApi() const;

    /*
     * Enqueues a task to the dumpstate's TaskQueue if the parallel run is enabled,
     * otherwise invokes it immediately. The task adds file at path entry_path
     * as a zip file entry with name entry_name. Unlinks entry_path when done.
     *
     * All enqueued tasks will be executed in the dumpstate's FinishZipFile method
     * before the zip file is finished. Tasks will be cancelled in dumpstate's
     * ShutdownDumpPool method if they have never been called.
     */
    void EnqueueAddZipEntryAndCleanupIfNeeded(const std::string& entry_name,
            const std::string& entry_path);

    /*
     * Structure to hold options that determine the behavior of dumpstate.
     */
@@ -495,6 +517,10 @@ class Dumpstate {
    // A thread pool to execute dump tasks simultaneously if the parallel run is enabled.
    std::unique_ptr<android::os::dumpstate::DumpPool> dump_pool_;

    // A task queue to collect adding zip entry tasks inside dump tasks if the
    // parallel run is enabled.
    std::unique_ptr<android::os::dumpstate::TaskQueue> zip_entry_tasks_;

    // A callback to IncidentCompanion service, which checks user consent for sharing the
    // bugreport with the calling app. If the user has not responded yet to the dialog it will
    // be neither confirmed nor denied.
@@ -548,6 +574,8 @@ class Dumpstate {

    android::sp<ConsentCallback> consent_callback_;

    std::recursive_mutex mutex_;

    DISALLOW_COPY_AND_ASSIGN(Dumpstate);
};

Loading