blob: 34489376e1e6e045b6ad8205f4d1890205175412 [file] [log] [blame]
/*
* Copyright (C) 2019 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 SRC_TRACE_PROCESSOR_RPC_RPC_H_
#define SRC_TRACE_PROCESSOR_RPC_RPC_H_
#include <functional>
#include <memory>
#include <vector>
#include <stddef.h>
#include <stdint.h>
#include "perfetto/ext/base/uuid.h"
#include "perfetto/trace_processor/status.h"
namespace perfetto {
namespace trace_processor {
class TraceProcessor;
// This class handles the binary {,un}marshalling for the Trace Processor RPC
// API (see protos/perfetto/trace_processor/trace_processor.proto).
// This is to deal with cases where the client of the trace processor is not
// some in-process C++ code but a remote process:
// There are two use cases of this:
// 1. The JS<>WASM interop for the web-based UI.
// 2. The HTTP RPC mode of trace_processor_shell that allows the UI to talk
// to a native trace processor instead of the bundled WASM one.
// This class has (a subset of) the same methods of the public TraceProcessor
// interface, but the methods just take and return proto-encoded binary buffers.
// This class does NOT define how the transport works (e.g. HTTP vs WASM interop
// calls), it just deals with {,un}marshalling.
// This class internally creates and owns a TraceProcessor instance, which
// lifetime is tied to the lifetime of the Rpc instance.
class Rpc {
public:
// The unique_ptr argument is optional. If non-null it will adopt the passed
// instance and allow to directly query that. If null, a new instanace will be
// created internally by calling Parse().
explicit Rpc(std::unique_ptr<TraceProcessor>);
Rpc();
~Rpc();
// The methods of this class are mirrors (modulo {un,}marshalling of args) of
// the corresponding names in trace_processor.h . See that header for docs.
util::Status Parse(const uint8_t* data, size_t len);
void NotifyEndOfFile();
std::string GetCurrentTraceName();
std::vector<uint8_t> ComputeMetric(const uint8_t* data, size_t len);
std::vector<uint8_t> GetMetricDescriptors(const uint8_t* data, size_t len);
void EnableMetatrace();
std::vector<uint8_t> DisableAndReadMetatrace();
// Creates a new RPC session by:
// a) deleting all tables and views that have been created (by the UI or user)
// after the trace was loaded; built-in tables/view created
// by the ingestion process are preserved.
// b) creates a new session id (see |GetSessionId| for more information).
//
// Historical note: the name of this function is |RestoreInitialTables|
// because it was created before the concept of an RPC session was
// defined when this function only reset the views. The scope was expanded
// rather than just creating a new function to preserve backward compatibility
// for clients.
void RestoreInitialTables();
// Returns the id of the RPC session. This id is an opaque string
// which can be used to globally and uniquely identify a particular session of
// RPC class.
//
// A new session is started (and thus a new session id is generated) when
// either:
// a) a new RPC instance is created
// b) |RestoreInitialTables| is called on an existing instance
//
// This can be used by RPC clients to determine whether they are talking to
// same instance they previously used to create tables/views.
std::string GetSessionId();
// Runs a query and returns results in batch. Each batch is a proto-encoded
// TraceProcessor.QueryResult message and contains a variable number of rows.
// The callbacks are called inline, so the whole callstack looks as follows:
// Query(..., callback)
// callback(..., has_more=true)
// ...
// callback(..., has_more=false)
// (Query() returns at this point).
// TODO(primiano): long-term this API should change and be turned into a
// bidirectional streaming api (see go/imperative-metrics). The problem with
// the current design is that it holds the callstack until the query is done
// and makes nested query hard as they cause re-entrancy. It's okay for now
// but will change soon.
using QueryResultBatchCallback = std::function<
void(const uint8_t* /*buf*/, size_t /*len*/, bool /*has_more*/)>;
void Query(const uint8_t* args, size_t len, QueryResultBatchCallback);
// DEPRECATED, only for legacy clients. Use |Query()| above.
std::vector<uint8_t> RawQuery(const uint8_t* args, size_t len);
private:
void MaybePrintProgress();
std::unique_ptr<TraceProcessor> trace_processor_;
bool eof_ = true; // Reset when calling Parse().
int64_t t_parse_started_ = 0;
size_t bytes_last_progress_ = 0;
size_t bytes_parsed_ = 0;
base::Uuid session_id_;
};
} // namespace trace_processor
} // namespace perfetto
#endif // SRC_TRACE_PROCESSOR_RPC_RPC_H_