File: update_memprof_inputs.sh

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (89 lines) | stat: -rwxr-xr-x 2,453 bytes parent folder | download | duplicates (2)
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
#!/bin/bash

if [ -z $1 ]; then
  echo "Path to clang required!"
  echo "Usage: update_memprof_inputs.sh /path/to/updated/clang"
  exit 1
else
  CLANG=$1
fi

# Allows the script to be invoked from other directories.
OUTDIR=$(dirname $(realpath -s $0))

# Note that changes in the code below which affect relative line number
# offsets of calls from their parent function can affect callsite matching in
# the LLVM IR.
cat > ${OUTDIR}/memprof.cc << EOF
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *foo() {
  return new char[10];
}
char *foo2() {
  return foo();
}
char *bar() {
  return foo2();
}
char *baz() {
  return foo2();
}
char *recurse(unsigned n) {
  if (!n)
    return foo();
  return recurse(n-1);
}
int main(int argc, char **argv) {
  // Test allocations with different combinations of stack contexts and
  // coldness (based on lifetime, since they are all accessed a single time
  // per byte via the memset).
  char *a = new char[10];
  char *b = new char[10];
  char *c = foo();
  char *d = foo();
  char *e = bar();
  char *f = baz();
  memset(a, 0, 10);
  memset(b, 0, 10);
  memset(c, 0, 10);
  memset(d, 0, 10);
  memset(e, 0, 10);
  memset(f, 0, 10);
  // a and c have short lifetimes
  delete[] a;
  delete[] c;
  // b, d, e, and f have long lifetimes and will be detected as cold by default.
  sleep(200);
  delete[] b;
  delete[] d;
  delete[] e;
  delete[] f;

  // Loop ensures the two calls to recurse have stack contexts that only differ
  // in one level of recursion. We should get two stack contexts reflecting the
  // different levels of recursion and different allocation behavior (since the
  // first has a very long lifetime and the second has a short lifetime).
  for (unsigned i = 0; i < 2; i++) {
    char *g = recurse(i + 3);
    memset(g, 0, 10);
    if (!i)
      sleep(200);
    delete[] g;
  }
  return 0;
}
EOF

COMMON_FLAGS="-fuse-ld=lld -Wl,--no-rosegment -gmlt -fdebug-info-for-profiling -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -m64 -Wl,-build-id -no-pie"

${CLANG} ${COMMON_FLAGS} -fmemory-profile ${OUTDIR}/memprof.cc -o ${OUTDIR}/memprof.exe
env MEMPROF_OPTIONS=log_path=stdout ${OUTDIR}/memprof.exe > ${OUTDIR}/memprof.memprofraw

${CLANG} ${COMMON_FLAGS} -fprofile-generate=. \
  ${OUTDIR}/memprof.cc -o ${OUTDIR}/pgo.exe
env LLVM_PROFILE_FILE=${OUTDIR}/memprof_pgo.profraw ${OUTDIR}/pgo.exe

rm ${OUTDIR}/memprof.cc
rm ${OUTDIR}/pgo.exe