1#!/bin/bash
2# Copyright (C) 2019 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
16DIR="$( cd "$(dirname "$0")" ; pwd -P )"
17
18if [[ "$#" -lt 3 ]]; then
19  echo "Usage: $0 <trace-dir> <db-dir> <output.csv>" >&2
20  exit 1
21fi
22
23simulate="n"
24
25TRACE_DIRNAME="$1"
26SQLITE_DIRNAME="$2"
27OUTPUT_FILENAME="$3"
28
29echo "Trace filename: $TRACE_DIRNAME"
30echo "SQLite filename: $SQLITE_DIRNAME"
31
32if ! [[ -d "$TRACE_DIRNAME" ]]; then
33  echo "Error: Trace '$TRACE_DIRNAME' does not exist." >&2
34  exit 1
35fi
36
37process_trace_file() {
38  local trace_filename="$1"
39  local db_dirname="$2"
40  local output_file="$3"
41
42  local db_filename="$db_dirname/$(basename "$trace_filename").db"
43
44  if [[ $simulate == y ]]; then
45    echo "$DIR/trace_analyzer.py" "$db_filename" "$trace_filename" "> /dev/null"
46  else
47    if ! "$DIR/trace_analyzer.py" "$db_filename" "$trace_filename" > /dev/null; then
48      echo "Fatal: trace_analyzer.py failed, aborting." >&2
49      return 1
50    fi
51  fi
52
53  if [[ $simulate == y ]]; then
54    echo "$DIR/run-sql-queries" "$db_filename" ">> '$output_file'"
55  else
56    # append name of trace to CSV, so we can see where data came from
57    echo "; $trace_filename" >> "$output_file"
58    if ! "$DIR/run-sql-queries" "$db_filename" >> "$output_file"; then
59      echo "Fatal: Failed to run sql queries, aborting." >&2
60      return 1
61    fi
62  fi
63
64  return 0
65}
66
67find "$TRACE_DIRNAME" -type f -name '*.trace' -print0 |
68while IFS= read -r -d '' file; do
69  if [[ $file == *#*.trace && $file != *#1.trace ]]; then
70    echo "Skip $file"
71    continue
72  fi
73
74  printf '%s\n' "$file"
75  process_trace_file "$file" "$SQLITE_DIRNAME" "$OUTPUT_FILENAME"
76done
77
78echo "Done"
79