• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <sys/ioctl.h>
17 #include <cstdio>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <termios.h>
23 #include "util.h"
24 
25 #define DEVICE_FILE "/dev/tty"
26 
27 // Used to perform control operations on device files
28 // set baud rate
Bm_function_Ioctl_baudrate(benchmark::State & state)29 static void Bm_function_Ioctl_baudrate(benchmark::State &state)
30 {
31     struct termios ttydev;
32     int ret;
33     int fd = open(DEVICE_FILE, O_RDWR, OPEN_MODE);
34     if (fd == -1) {
35         perror("open ioctl");
36     }
37     ret = tcgetattr(fd, &ttydev);
38     if (ret == -1) {
39         perror("Property fetch failed");
40     }
41     int speed = cfgetospeed(&ttydev);
42     cfsetospeed(&ttydev, B19200);
43     cfsetispeed(&ttydev, B19200);
44     for (auto _ : state) {
45         ret = ioctl(fd, TCSETS, &ttydev);
46         if (ret < 0) {
47             perror("ioctl");
48         }
49         benchmark::DoNotOptimize(ret);
50     }
51     cfsetospeed(&ttydev, speed);
52     cfsetispeed(&ttydev, speed);
53     ret = ioctl(fd, TCSETS, &ttydev);
54     if (ret == -1) {
55         perror("Failed to restore baud rate");
56     }
57     close(fd);
58 }
59 
60 MUSL_BENCHMARK(Bm_function_Ioctl_baudrate);
61