update_kernel_afdo: Update arm afdo metadata

BUG=b:244337204
TEST=./update_kernel_afdo

Change-Id: I47722a6c3e4446bff816baed7711279dd923a9c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3994209
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Commit-Queue: Denis Nikitin <denik@chromium.org>
Tested-by: Denis Nikitin <denik@chromium.org>
diff --git a/afdo_tools/update_kernel_afdo b/afdo_tools/update_kernel_afdo
index e8e583e..be0fa8d 100755
--- a/afdo_tools/update_kernel_afdo
+++ b/afdo_tools/update_kernel_afdo
@@ -24,8 +24,10 @@
 set -eu
 set -o pipefail
 
-GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel
-KVERS="4.14 4.19 5.4 5.10"
+AMD_GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel
+ARM_GS_BASE=gs://chromeos-prebuilt/afdo-job/vetted/kernel/arm
+AMD_KVERS="4.14 4.19 5.4 5.10"
+ARM_KVERS="5.15"
 failed_channels=""
 # Add skipped chrome branches in ascending order here.
 SKIPPED_BRANCHES="95"
@@ -49,14 +51,25 @@
 script_dir=$(dirname "$0")
 tc_utils_dir="${script_dir}/.."
 metadata_dir="${tc_utils_dir}/afdo_metadata"
-outfile="$(realpath --relative-to="${tc_utils_dir}" \
+amd_outfile="$(realpath --relative-to="${tc_utils_dir}" \
   "${metadata_dir}"/kernel_afdo.json)"
+arm_outfile="$(realpath --relative-to="${tc_utils_dir}" \
+  "${metadata_dir}"/kernel_arm_afdo.json)"
 # Convert toolchain_utils into the absolute path.
 abs_tc_utils_dir="$(realpath "${tc_utils_dir}")"
 
 # Check profiles uploaded within the last week.
 expected_time=$(date +%s -d "week ago")
 
+ARCHS="amd arm"
+declare -A arch_gsbase arch_kvers arch_outfile
+arch_gsbase["amd"]="${AMD_GS_BASE}"
+arch_gsbase["arm"]="${ARM_GS_BASE}"
+arch_kvers["amd"]="${AMD_KVERS}"
+arch_kvers["arm"]="${ARM_KVERS}"
+arch_outfile["amd"]="${amd_outfile}"
+arch_outfile["arm"]="${arm_outfile}"
+
 declare -A branch branch_number commit
 remote_repo=$(git -C "${tc_utils_dir}" remote)
 canary_ref="refs/heads/main"
@@ -126,112 +139,122 @@
   echo
   echo "Checking \"${channel}\" channel..."
   echo "branch_number=${curr_branch_number} branch=${curr_branch}"
-  json="{"
-  sep=""
-  for kver in ${KVERS}
+
+  git reset --hard HEAD
+  git checkout "${remote_repo}/${curr_branch}"
+
+  for arch in ${ARCHS}
   do
-    # Skip kernels disabled in this branch.
-    skipped=false
-    for skipped_branch in "${!SKIPPED_KVERS_IN_BRANCHES[@]}"
+    json="{"
+    sep=""
+    for kver in ${arch_kvers[${arch}]}
     do
-      if [[ ${curr_branch_number} == "${skipped_branch}" ]]
+      # Skip kernels disabled in this branch.
+      skipped=false
+      for skipped_branch in "${!SKIPPED_KVERS_IN_BRANCHES[@]}"
+      do
+        if [[ ${curr_branch_number} == "${skipped_branch}" ]]
+        then
+          # Current branch is in the keys of SKIPPED_KVERS_IN_BRANCHES.
+          # Now lets check if $kver is in the list.
+          for skipped_kver in ${SKIPPED_KVERS_IN_BRANCHES[${skipped_branch}]}
+          do
+            if [[ ${kver} == "${skipped_kver}" ]]
+            then
+              skipped=true
+              break
+            fi
+          done
+        fi
+      done
+      if ${skipped}
       then
-        # Current branch is in the keys of SKIPPED_KVERS_IN_BRANCHES.
-        # Now lets check if $kver is in the list.
-        for skipped_kver in ${SKIPPED_KVERS_IN_BRANCHES[${skipped_branch}]}
-        do
-          if [[ ${kver} == "${skipped_kver}" ]]
-          then
-            skipped=true
-            break
-          fi
-        done
+        echo "${kver} is skipped in branch ${curr_branch_number}. Skip it."
+        continue
       fi
-    done
-    if ${skipped}
-    then
-      echo "${kver} is skipped in branch ${curr_branch_number}. Skip it."
-      continue
-    fi
-    # 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${curr_branch_number}" | tail -1 || true)
-    if [[ -z "${latest}" && "${channel}" != "stable" ]]
-    then
-      # if no profiles exist for the current branch, try the previous branch
-      latest=$(gsutil.py ls -l "${GS_BASE}/${kver}/" | sort -k2 | \
-        grep "R$((curr_branch_number - 1))" | tail -1)
-    fi
+      # 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 "${arch_gsbase[${arch}]}/${kver}/" | sort -k2 | \
+               grep "R${curr_branch_number}" | tail -1 || true)
+      if [[ -z "${latest}" && "${channel}" != "stable" ]]
+      then
+        # if no profiles exist for the current branch, try the previous branch
+        latest=$(gsutil.py ls -l "${arch_gsbase[${arch}]}/${kver}/" | \
+          sort -k2 | grep "R$((curr_branch_number - 1))" | 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
+      # 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="$(basename "${latest%.gcov.*}")"
-    # Skip kernels with no AFDO support in the current channel.
-    if [[ "${name}" == "" ]]
-    then
-      continue
-    fi
-    json=$(cat <<EOT
+      # 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="$(basename "${latest%.gcov.*}")"
+      # Skip kernels with no AFDO support in the current channel.
+      if [[ "${name}" == "" ]]
+      then
+        continue
+      fi
+      json=$(cat <<EOT
 ${json}${sep}
     "chromeos-kernel-${json_kver}": {
         "name": "${name}"
     }
 EOT
-    )
-    sep=","
-    successes=$((successes + 1))
-  done
+      )
+      sep=","
+      successes=$((successes + 1))
+    done # kvers loop
 
-  # 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
-    failed_channels="${failed_channels} ${channel}"
-    continue
-  fi
+    # 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
+      failed_channels="${failed_channels} ${channel}"
+      continue
+    fi
 
-  git reset --hard HEAD
-  echo git checkout "${remote_repo}/${curr_branch}"
-  git checkout "${remote_repo}/${curr_branch}"
+    # Write new JSON file.
+    # Don't use `echo` since `json` might have esc characters in it.
+    printf "%s\n}\n" "${json}" > "${arch_outfile[${arch}]}"
 
-  # Write new JSON file.
-  # Don't use `echo` since `json` might have esc characters in it.
-  printf "%s\n}\n" "${json}" > "${outfile}"
+    # If no changes were made, say so.
+    outdir=$(dirname "${arch_outfile[${arch}]}")
+    shortstat=$(cd "${outdir}" &&\
+      git status --short "$(basename "${arch_outfile[${arch}]}")")
+    [ -z "${shortstat}" ] &&\
+      echo "$(basename "${arch_outfile[${arch}]}") is up to date." \
+      && continue
 
-  # If no changes were made, say so.
-  outdir=$(dirname "${outfile}")
-  shortstat=$(cd "${outdir}" && git status --short "$(basename "${outfile}")")
-  [ -z "${shortstat}" ] && echo "$(basename "${outfile}") is up to date." \
-    && continue
+    # If we had any errors, warn about them.
+    if [[ -n "${errs}" ]]
+    then
+      echo "warning: failed to update ${errs} in ${channel}" >&2
+      failed_channels="${failed_channels} ${channel}"
+      continue
+    fi
 
-  # If we had any errors, warn about them.
-  if [[ -n "${errs}" ]]
-  then
-    echo "warning: failed to update ${errs} in ${channel}" >&2
-    failed_channels="${failed_channels} ${channel}"
-    continue
-  fi
+    git add "${arch_outfile[${arch}]}"
+  done # ARCHS loop
 
-  git add afdo_metadata/kernel_afdo.json
   case "${channel}" in
     canary )
       commit_contents=$'afdo_metadata: Publish the new kernel profiles\n\n'
-      for kver in ${KVERS} ; do
-        commit_contents="${commit_contents}Update chromeos-kernel-${kver}"$'\n'
+      for arch in ${ARCHS} ; do
+        for kver in ${arch_kvers[${arch}]} ; do
+          commit_contents="${commit_contents}Update ${arch} profile on\
+ chromeos-kernel-${kver}"$'\n'
+        done
       done
       commit_contents="${commit_contents}