~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to .gitlab-ci/common/intel-gpu-freq.sh

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
#
3
 
# The Intel i915 GPU driver allows to change the minimum, maximum and boost
4
 
# frequencies in steps of 50 MHz via /sys/class/drm/card<n>/<freq_info>,
5
 
# where <n> is the DRM card index and <freq_info> one of the following:
6
 
#
7
 
# - gt_max_freq_mhz (enforced maximum freq)
8
 
# - gt_min_freq_mhz (enforced minimum freq)
9
 
# - gt_boost_freq_mhz (enforced boost freq)
10
 
#
11
 
# The hardware capabilities can be accessed via:
12
 
#
13
 
# - gt_RP0_freq_mhz (supported maximum freq)
14
 
# - gt_RPn_freq_mhz (supported minimum freq)
15
 
# - gt_RP1_freq_mhz (most efficient freq)
16
 
#
17
 
# The current frequency can be read from:
18
 
# - gt_act_freq_mhz (the actual GPU freq)
19
 
# - gt_cur_freq_mhz (the last requested freq)
20
 
#
21
 
# Copyright (C) 2022 Collabora Ltd.
22
 
# Author: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
23
 
#
24
 
# SPDX-License-Identifier: MIT
25
 
#
26
 
 
27
 
#
28
 
# Constants
29
 
#
30
 
DRM_FREQ_SYSFS_PATTERN="/sys/class/drm/card%d/gt_%s_freq_mhz"
31
 
ENF_FREQ_INFO="max min boost"
32
 
CAP_FREQ_INFO="RP0 RPn RP1"
33
 
ACT_FREQ_INFO="act cur"
34
 
THROTT_DETECT_SLEEP_SEC=2
35
 
THROTT_DETECT_PID_FILE_PATH=/tmp/thrott-detect.pid
36
 
 
37
 
#
38
 
# Global variables.
39
 
#
40
 
unset INTEL_DRM_CARD_INDEX
41
 
unset GET_ACT_FREQ GET_ENF_FREQ GET_CAP_FREQ
42
 
unset SET_MIN_FREQ SET_MAX_FREQ
43
 
unset MONITOR_FREQ
44
 
unset DETECT_THROTT
45
 
unset DRY_RUN
46
 
 
47
 
#
48
 
# Simple printf based stderr logger.
49
 
#
50
 
log() {
51
 
    local msg_type=$1
52
 
 
53
 
    shift
54
 
    printf "%s: %s: " "${msg_type}" "${0##*/}" >&2
55
 
    printf "$@" >&2
56
 
    printf "\n" >&2
57
 
}
58
 
 
59
 
#
60
 
# Helper to print sysfs path for the given card index and freq info.
61
 
#
62
 
# arg1: Frequency info sysfs name, one of *_FREQ_INFO constants above
63
 
# arg2: Video card index, defaults to INTEL_DRM_CARD_INDEX
64
 
#
65
 
print_freq_sysfs_path() {
66
 
    printf ${DRM_FREQ_SYSFS_PATTERN} "${2:-${INTEL_DRM_CARD_INDEX}}" "$1"
67
 
}
68
 
 
69
 
#
70
 
# Helper to set INTEL_DRM_CARD_INDEX for the first identified Intel video card.
71
 
#
72
 
identify_intel_gpu() {
73
 
    local i=0 vendor path
74
 
 
75
 
    while [ ${i} -lt 16 ]; do
76
 
        [ -c "/dev/dri/card$i" ] || {
77
 
            i=$((i + 1))
78
 
            continue
79
 
        }
80
 
 
81
 
        path=$(print_freq_sysfs_path "" ${i})
82
 
        path=${path%/*}/device/vendor
83
 
 
84
 
        [ -r "${path}" ] && read vendor < "${path}" && \
85
 
            [ "${vendor}" = "0x8086" ] && INTEL_DRM_CARD_INDEX=$i && return 0
86
 
 
87
 
        i=$((i + 1))
88
 
    done
89
 
 
90
 
    return 1
91
 
}
92
 
 
93
 
#
94
 
# Read the specified freq info from sysfs.
95
 
#
96
 
# arg1: Flag (y/n) to also enable printing the freq info.
97
 
# arg2...: Frequency info sysfs name(s), see *_FREQ_INFO constants above
98
 
# return: Global variable(s) FREQ_${arg} containing the requested information
99
 
#
100
 
read_freq_info() {
101
 
    local var val path print=0 ret=0
102
 
 
103
 
    [ "$1" = "y" ] && print=1
104
 
    shift
105
 
 
106
 
    while [ $# -gt 0 ]; do
107
 
        var=FREQ_$1
108
 
        path=$(print_freq_sysfs_path "$1")
109
 
 
110
 
        [ -r ${path} ] && read ${var} < ${path} || {
111
 
            log ERROR "Failed to read freq info from: %s" "${path}"
112
 
            ret=1
113
 
            continue
114
 
        }
115
 
 
116
 
        [ -n "${var}" ] || {
117
 
            log ERROR "Got empty freq info from: %s" "${path}"
118
 
            ret=1
119
 
            continue
120
 
        }
121
 
 
122
 
        [ ${print} -eq 1 ] && {
123
 
            eval val=\$${var}
124
 
            printf "%6s: %4s MHz\n" "$1" "${val}"
125
 
        }
126
 
 
127
 
        shift
128
 
    done
129
 
 
130
 
    return ${ret}
131
 
}
132
 
 
133
 
#
134
 
# Display requested info.
135
 
#
136
 
print_freq_info() {
137
 
    local req_freq
138
 
 
139
 
    [ -n "${GET_CAP_FREQ}" ] && {
140
 
        printf "* Hardware capabilities\n"
141
 
        read_freq_info y ${CAP_FREQ_INFO}
142
 
        printf "\n"
143
 
    }
144
 
 
145
 
    [ -n "${GET_ENF_FREQ}" ] && {
146
 
        printf "* Enforcements\n"
147
 
        read_freq_info y ${ENF_FREQ_INFO}
148
 
        printf "\n"
149
 
    }
150
 
 
151
 
    [ -n "${GET_ACT_FREQ}" ] && {
152
 
        printf "* Actual\n"
153
 
        read_freq_info y ${ACT_FREQ_INFO}
154
 
        printf "\n"
155
 
    }
156
 
}
157
 
 
158
 
#
159
 
# Helper to print frequency value as requested by user via '-s, --set' option.
160
 
# arg1: user requested freq value
161
 
#
162
 
compute_freq_set() {
163
 
    local val
164
 
 
165
 
    case "$1" in
166
 
    +)
167
 
        val=${FREQ_RP0}
168
 
        ;;
169
 
    -)
170
 
        val=${FREQ_RPn}
171
 
        ;;
172
 
    *%)
173
 
        val=$((${1%?} * ${FREQ_RP0} / 100))
174
 
        # Adjust freq to comply with 50 MHz increments
175
 
        val=$((val / 50 * 50))
176
 
        ;;
177
 
    *[!0-9]*)
178
 
        log ERROR "Cannot set freq to invalid value: %s" "$1"
179
 
        return 1
180
 
        ;;
181
 
    "")
182
 
        log ERROR "Cannot set freq to unspecified value"
183
 
        return 1
184
 
        ;;
185
 
    *)
186
 
        # Adjust freq to comply with 50 MHz increments
187
 
        val=$(($1 / 50 * 50))
188
 
        ;;
189
 
    esac
190
 
 
191
 
    printf "%s" "${val}"
192
 
}
193
 
 
194
 
#
195
 
# Helper for set_freq().
196
 
#
197
 
set_freq_max() {
198
 
    log INFO "Setting GPU max freq to %s MHz" "${SET_MAX_FREQ}"
199
 
 
200
 
    read_freq_info n min || return $?
201
 
 
202
 
    [ ${SET_MAX_FREQ} -gt ${FREQ_RP0} ] && {
203
 
        log ERROR "Cannot set GPU max freq (%s) to be greater than hw max freq (%s)" \
204
 
            "${SET_MAX_FREQ}" "${FREQ_RP0}"
205
 
        return 1
206
 
    }
207
 
 
208
 
    [ ${SET_MAX_FREQ} -lt ${FREQ_RPn} ] && {
209
 
        log ERROR "Cannot set GPU max freq (%s) to be less than hw min freq (%s)" \
210
 
            "${SET_MIN_FREQ}" "${FREQ_RPn}"
211
 
        return 1
212
 
    }
213
 
 
214
 
    [ ${SET_MAX_FREQ} -lt ${FREQ_min} ] && {
215
 
        log ERROR "Cannot set GPU max freq (%s) to be less than min freq (%s)" \
216
 
            "${SET_MAX_FREQ}" "${FREQ_min}"
217
 
        return 1
218
 
    }
219
 
 
220
 
    [ -z "${DRY_RUN}" ] || return 0
221
 
 
222
 
    printf "%s" ${SET_MAX_FREQ} | tee $(print_freq_sysfs_path max) \
223
 
        $(print_freq_sysfs_path boost) > /dev/null
224
 
    [ $? -eq 0 ] || {
225
 
        log ERROR "Failed to set GPU max frequency"
226
 
        return 1
227
 
    }
228
 
}
229
 
 
230
 
#
231
 
# Helper for set_freq().
232
 
#
233
 
set_freq_min() {
234
 
    log INFO "Setting GPU min freq to %s MHz" "${SET_MIN_FREQ}"
235
 
 
236
 
    read_freq_info n max || return $?
237
 
 
238
 
    [ ${SET_MIN_FREQ} -gt ${FREQ_max} ] && {
239
 
        log ERROR "Cannot set GPU min freq (%s) to be greater than max freq (%s)" \
240
 
            "${SET_MIN_FREQ}" "${FREQ_max}"
241
 
        return 1
242
 
    }
243
 
 
244
 
    [ ${SET_MIN_FREQ} -lt ${FREQ_RPn} ] && {
245
 
        log ERROR "Cannot set GPU min freq (%s) to be less than hw min freq (%s)" \
246
 
            "${SET_MIN_FREQ}" "${FREQ_RPn}"
247
 
        return 1
248
 
    }
249
 
 
250
 
    [ -z "${DRY_RUN}" ] || return 0
251
 
 
252
 
    printf "%s" ${SET_MIN_FREQ} > $(print_freq_sysfs_path min)
253
 
    [ $? -eq 0 ] || {
254
 
        log ERROR "Failed to set GPU min frequency"
255
 
        return 1
256
 
    }
257
 
}
258
 
 
259
 
#
260
 
# Set min or max or both GPU frequencies to the user indicated values.
261
 
#
262
 
set_freq() {
263
 
    # Get hw max & min frequencies
264
 
    read_freq_info n RP0 RPn || return $?
265
 
 
266
 
    [ -z "${SET_MAX_FREQ}" ] || {
267
 
        SET_MAX_FREQ=$(compute_freq_set "${SET_MAX_FREQ}")
268
 
        [ -z "${SET_MAX_FREQ}" ] && return 1
269
 
    }
270
 
 
271
 
    [ -z "${SET_MIN_FREQ}" ] || {
272
 
        SET_MIN_FREQ=$(compute_freq_set "${SET_MIN_FREQ}")
273
 
        [ -z "${SET_MIN_FREQ}" ] && return 1
274
 
    }
275
 
 
276
 
    #
277
 
    # Ensure correct operation order, to avoid setting min freq
278
 
    # to a value which is larger than max freq.
279
 
    #
280
 
    # E.g.:
281
 
    #   crt_min=crt_max=600; new_min=new_max=700
282
 
    #   > operation order: max=700; min=700
283
 
    #
284
 
    #   crt_min=crt_max=600; new_min=new_max=500
285
 
    #   > operation order: min=500; max=500
286
 
    #
287
 
    if [ -n "${SET_MAX_FREQ}" ] && [ -n "${SET_MIN_FREQ}" ]; then
288
 
        [ ${SET_MAX_FREQ} -lt ${SET_MIN_FREQ} ] && {
289
 
            log ERROR "Cannot set GPU max freq to be less than min freq"
290
 
            return 1
291
 
        }
292
 
 
293
 
        read_freq_info n min || return $?
294
 
 
295
 
        if [ ${SET_MAX_FREQ} -lt ${FREQ_min} ]; then
296
 
            set_freq_min || return $?
297
 
            set_freq_max
298
 
        else
299
 
            set_freq_max || return $?
300
 
            set_freq_min
301
 
        fi
302
 
    elif [ -n "${SET_MAX_FREQ}" ]; then
303
 
        set_freq_max
304
 
    elif [ -n "${SET_MIN_FREQ}" ]; then
305
 
        set_freq_min
306
 
    else
307
 
        log "Unexpected call to set_freq()"
308
 
        return 1
309
 
    fi
310
 
}
311
 
 
312
 
#
313
 
# Helper for detect_throttling().
314
 
#
315
 
get_thrott_detect_pid() {
316
 
    [ -e ${THROTT_DETECT_PID_FILE_PATH} ] || return 0
317
 
 
318
 
    local pid
319
 
    read pid < ${THROTT_DETECT_PID_FILE_PATH} || {
320
 
        log ERROR "Failed to read pid from: %s" "${THROTT_DETECT_PID_FILE_PATH}"
321
 
        return 1
322
 
    }
323
 
 
324
 
    local proc_path=/proc/${pid:-invalid}/cmdline
325
 
    [ -r ${proc_path} ] && grep -qs "${0##*/}" ${proc_path} && {
326
 
        printf "%s" "${pid}"
327
 
        return 0
328
 
    }
329
 
 
330
 
    # Remove orphaned PID file
331
 
    rm -rf ${THROTT_DETECT_PID_FILE_PATH}
332
 
    return 1
333
 
}
334
 
 
335
 
#
336
 
# Control detection and reporting of GPU throttling events.
337
 
# arg1: start - run throttle detector in background
338
 
#       stop - stop throttle detector process, if any
339
 
#       status - verify if throttle detector is running
340
 
#
341
 
detect_throttling() {
342
 
    local pid
343
 
    pid=$(get_thrott_detect_pid)
344
 
 
345
 
    case "$1" in
346
 
    status)
347
 
        printf "Throttling detector is "
348
 
        [ -z "${pid}" ] && printf "not running\n" && return 0
349
 
        printf "running (pid=%s)\n" ${pid}
350
 
        ;;
351
 
 
352
 
    stop)
353
 
        [ -z "${pid}" ] && return 0
354
 
 
355
 
        log INFO "Stopping throttling detector (pid=%s)" "${pid}"
356
 
        kill ${pid}; sleep 1; kill -0 ${pid} 2>/dev/null && kill -9 ${pid}
357
 
        rm -rf ${THROTT_DETECT_PID_FILE_PATH}
358
 
        ;;
359
 
 
360
 
    start)
361
 
        [ -n "${pid}" ] && {
362
 
            log WARN "Throttling detector is already running (pid=%s)" ${pid}
363
 
            return 0
364
 
        }
365
 
 
366
 
        (
367
 
            read_freq_info n RPn || exit $?
368
 
 
369
 
            while true; do
370
 
                sleep ${THROTT_DETECT_SLEEP_SEC}
371
 
                read_freq_info n act min cur || exit $?
372
 
 
373
 
                #
374
 
                # The throttling seems to occur when act freq goes below min.
375
 
                # However, it's necessary to exclude the idle states, where
376
 
                # act freq normally reaches RPn and cur goes below min.
377
 
                #
378
 
                [ ${FREQ_act} -lt ${FREQ_min} ] && \
379
 
                [ ${FREQ_act} -gt ${FREQ_RPn} ] && \
380
 
                [ ${FREQ_cur} -ge ${FREQ_min} ] && \
381
 
                    printf "GPU throttling detected: act=%s min=%s cur=%s RPn=%s\n" \
382
 
                    ${FREQ_act} ${FREQ_min} ${FREQ_cur} ${FREQ_RPn}
383
 
            done
384
 
        ) &
385
 
 
386
 
        pid=$!
387
 
        log INFO "Started GPU throttling detector (pid=%s)" ${pid}
388
 
 
389
 
        printf "%s\n" ${pid} > ${THROTT_DETECT_PID_FILE_PATH} || \
390
 
            log WARN "Failed to write throttle detector PID file"
391
 
        ;;
392
 
    esac
393
 
}
394
 
 
395
 
#
396
 
# Show help message.
397
 
#
398
 
print_usage() {
399
 
    cat <<EOF
400
 
Usage: ${0##*/} [OPTION]...
401
 
 
402
 
A script to manage Intel GPU frequencies. Can be used for debugging performance
403
 
problems or trying to obtain a stable frequency while benchmarking.
404
 
 
405
 
Note Intel GPUs only accept specific frequencies, usually multiples of 50 MHz.
406
 
 
407
 
Options:
408
 
  -g, --get [act|enf|cap|all]
409
 
                        Get frequency information: active (default), enforced,
410
 
                        hardware capabilities or all of them.
411
 
 
412
 
  -s, --set [{min|max}=]{FREQUENCY[%]|+|-}
413
 
                        Set min or max frequency to the given value (MHz).
414
 
                        Append '%' to interpret FREQUENCY as % of hw max.
415
 
                        Use '+' or '-' to set frequency to hardware max or min.
416
 
                        Omit min/max prefix to set both frequencies.
417
 
 
418
 
  -r, --reset           Reset frequencies to hardware defaults.
419
 
 
420
 
  -m, --monitor [act|enf|cap|all]
421
 
                        Monitor the indicated frequencies via 'watch' utility.
422
 
                        See '-g, --get' option for more details.
423
 
 
424
 
  -d|--detect-thrott [start|stop|status]
425
 
                        Start (default operation) the throttling detector
426
 
                        as a background process. Use 'stop' or 'status' to
427
 
                        terminate the detector process or verify its status.
428
 
 
429
 
  --dry-run             See what the script will do without applying any
430
 
                        frequency changes.
431
 
 
432
 
  -h, --help            Display this help text and exit.
433
 
EOF
434
 
}
435
 
 
436
 
#
437
 
# Parse user input for '-g, --get' option.
438
 
# Returns 0 if a value has been provided, otherwise 1.
439
 
#
440
 
parse_option_get() {
441
 
    local ret=0
442
 
 
443
 
    case "$1" in
444
 
    act) GET_ACT_FREQ=1;;
445
 
    enf) GET_ENF_FREQ=1;;
446
 
    cap) GET_CAP_FREQ=1;;
447
 
    all) GET_ACT_FREQ=1; GET_ENF_FREQ=1; GET_CAP_FREQ=1;;
448
 
    -*|"")
449
 
        # No value provided, using default.
450
 
        GET_ACT_FREQ=1
451
 
        ret=1
452
 
        ;;
453
 
    *)
454
 
        print_usage
455
 
        exit 1
456
 
        ;;
457
 
    esac
458
 
 
459
 
    return ${ret}
460
 
}
461
 
 
462
 
#
463
 
# Validate user input for '-s, --set' option.
464
 
#
465
 
validate_option_set() {
466
 
    case "$1" in
467
 
    +|-|[0-9]%|[0-9][0-9]%)
468
 
        return 0
469
 
        ;;
470
 
    *[!0-9]*|"")
471
 
        print_usage
472
 
        exit 1
473
 
        ;;
474
 
    esac
475
 
}
476
 
 
477
 
#
478
 
# Parse script arguments.
479
 
#
480
 
[ $# -eq 0 ] && { print_usage; exit 1; }
481
 
 
482
 
while [ $# -gt 0 ]; do
483
 
    case "$1" in
484
 
    -g|--get)
485
 
        parse_option_get "$2" && shift
486
 
        ;;
487
 
 
488
 
    -s|--set)
489
 
        shift
490
 
        case "$1" in
491
 
        min=*)
492
 
            SET_MIN_FREQ=${1#min=}
493
 
            validate_option_set "${SET_MIN_FREQ}"
494
 
            ;;
495
 
        max=*)
496
 
            SET_MAX_FREQ=${1#max=}
497
 
            validate_option_set "${SET_MAX_FREQ}"
498
 
            ;;
499
 
        *)
500
 
            SET_MIN_FREQ=$1
501
 
            validate_option_set "${SET_MIN_FREQ}"
502
 
            SET_MAX_FREQ=${SET_MIN_FREQ}
503
 
            ;;
504
 
        esac
505
 
        ;;
506
 
 
507
 
    -r|--reset)
508
 
        RESET_FREQ=1
509
 
        SET_MIN_FREQ="-"
510
 
        SET_MAX_FREQ="+"
511
 
        ;;
512
 
 
513
 
    -m|--monitor)
514
 
        MONITOR_FREQ=act
515
 
        parse_option_get "$2" && MONITOR_FREQ=$2 && shift
516
 
        ;;
517
 
 
518
 
    -d|--detect-thrott)
519
 
        DETECT_THROTT=start
520
 
        case "$2" in
521
 
        start|stop|status)
522
 
            DETECT_THROTT=$2
523
 
            shift
524
 
            ;;
525
 
        esac
526
 
        ;;
527
 
 
528
 
    --dry-run)
529
 
        DRY_RUN=1
530
 
        ;;
531
 
 
532
 
    -h|--help)
533
 
        print_usage
534
 
        exit 0
535
 
        ;;
536
 
 
537
 
    *)
538
 
        print_usage
539
 
        exit 1
540
 
        ;;
541
 
    esac
542
 
 
543
 
    shift
544
 
done
545
 
 
546
 
#
547
 
# Main
548
 
#
549
 
RET=0
550
 
 
551
 
identify_intel_gpu || {
552
 
    log INFO "No Intel GPU detected"
553
 
    exit 0
554
 
}
555
 
 
556
 
[ -n "${SET_MIN_FREQ}${SET_MAX_FREQ}" ] && { set_freq || RET=$?; }
557
 
print_freq_info
558
 
 
559
 
[ -n "${DETECT_THROTT}" ] && detect_throttling ${DETECT_THROTT}
560
 
 
561
 
[ -n "${MONITOR_FREQ}" ] && {
562
 
    log INFO "Entering frequency monitoring mode"
563
 
    sleep 2
564
 
    exec watch -d -n 1 "$0" -g "${MONITOR_FREQ}"
565
 
}
566
 
 
567
 
exit ${RET}