aboutsummaryrefslogtreecommitdiff
path: root/afdo_tools/update_kernel_afdo
blob: a677bf1b3b3e841ca0c60cb480260477fb442f7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Due to crbug.com/1081332, we need to update AFDO metadata
# manually. This script performs a few checks and generates a
# new kernel_afdo.json file, which can then be committed.
#
# USAGE:
# toolchain-utils$ ./afdo_tools/update_kernel_afdo
#
# The script modifies the JSON file and shows the git diff.
#
# If the changes look good, git commit them. Example commit
# message (from crrev.com/c/2197462):
#
#   afdo_metadata: Publish the new kernel profiles
#
#   Update chromeos-kernel-3_18 to R84-13080.0-1589189810
#   Update chromeos-kernel-4_4 to R84-13080.0-1589189726
#   Update chromeos-kernel-4_14 to R84-13080.0-1589190025
#   Update chromeos-kernel-4_19 to R84-13080.0-1589189550
#
#   BUG=None
#   TEST=Verified in kernel-release-afdo-verify-orchestrator.
#

set -eu
set -o pipefail

CROS_REPO=https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay
GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel
KVERS="3.18 4.4 4.14 4.19"
errs=""
successes=0

script_dir=$(dirname "$0")
tc_utils_dir="$script_dir/.."
metadata_dir="$tc_utils_dir/afdo_metadata"
outfile="$metadata_dir/kernel_afdo.json"

# The most recent Monday, in Unix timestamp format.
if [ $(date +%a) = "Mon" ]
then
  expected_time=$(date +%s -d 00:00:00)
else
  expected_time=$(date +%s -d "last Monday")
fi

# Get the current canary branch number (using beta + 1)
beta=$(git ls-remote -h $CROS_REPO | \
       sed -n -e "s/^.*release-R\([0-9][0-9]*\).*$/\1/p" | \
       sort -g | tail -1)
canary="$(($beta + 1))"

json="{"
sep=""
for kver in $KVERS
do
  # Sort the gs output by timestamp (default ordering is by name, so
  # R86-13310.3-1594633089.gcov.xz goes after R86-13310.18-1595237847.gcov.xz)
  latest=$(gsutil.py ls -l "$GS_BASE/$kver/" | sort -k2 | \
           grep "R${canary}" | tail -1 || true)
  if [ -z "$latest" ]
  then
    # if no profiles exist for R${canary}, try the previous branch
    latest=$(gsutil.py ls -l "$GS_BASE/$kver/" | sort -k2 | \
             grep "R${beta}" | tail -1)
  fi

  # Verify that the file has the expected date.
  file_time=$(echo "$latest" | awk '{print $2}')
  file_time_unix=$(date +%s -d "$file_time")
  if [ $file_time_unix -lt $expected_time ]
  then
    expected=$(env TZ=UTC date +%Y-%m-%dT%H:%M:%SZ -d @$expected_time)
    echo "Wrong date for $kver: $file_time is before $expected" >&2
    errs="$errs $kver"
    continue
  fi

  # Generate JSON.
  json_kver=$(echo "$kver" | tr . _)
  # b/147370213 (migrating profiles from gcov format) may result in the
  # pattern below no longer doing the right thing.
  name=$(echo "$latest" | sed 's%.*/\(.*\)\.gcov.*%\1%')
  json=$(cat <<EOT
$json$sep
    "chromeos-kernel-$json_kver": {
        "name": "$name"
    }
EOT
)
  sep=","
  successes=$((successes + 1))
done

# If we did not succeed for any kvers, exit now.
if [ $successes -eq 0 ]
then
  echo "error: AFDO profiles out of date for all kernel versions" >&2
  exit 2
fi

# Write new JSON file.
printf "%s\n}\n" "$json" > "$outfile"

# Show the changes.
(cd "$tc_utils_dir" && git diff)

# If no changes were made, say so.
outdir=$(dirname "$outfile")
shortstat=$(cd "$outdir" && git status --short $(basename "$outfile"))
[ -n "$shortstat" ] || echo $(basename "$outfile")" is up to date."

# If we had any errors, warn about them.
[ -z "$errs" ] || echo "warning: failed to update$errs" >&2