187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller/*
287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Copyright (C) 2010 The Android Open Source Project
387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Licensed under the Apache License, Version 2.0 (the "License");
587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * you may not use this file except in compliance with the License.
687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * You may obtain a copy of the License at
787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *      http://www.apache.org/licenses/LICENSE-2.0
987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
1087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Unless required by applicable law or agreed to in writing, software
1187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * distributed under the License is distributed on an "AS IS" BASIS,
1287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * See the License for the specific language governing permissions and
1487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * limitations under the License.
1587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
1687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller */
1787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
1887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller/*
1987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * WiFi load, scan, associate, unload stress test
2087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
2187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Repeatedly executes the following sequence:
2287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
2387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   1. Load WiFi driver
2487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   2. Start supplicant
2587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   3. Random delay
2687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   4. Obtain supplicant status (optional)
2787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   5. Stop supplicant
2887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   6. Unload WiFi driver
2987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
3087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * The "Obtain supplicant status" step is optional and is pseudo
3187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * randomly performed 50% of the time.  The default range of
3287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * delay after start supplicant is intentionally selected such
3387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * that the obtain supplicant status and stop supplicant steps
3487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * may be performed while the WiFi driver is still performing a scan
3587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * or associate.  The default values are given by DEFAULT_DELAY_MIN
3687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * and DEFAULT_DELAY_MAX.  Other values can be specified through the
3787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * use of the -d and -D command-line options.
3887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
3987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Each sequence is refered to as a pass and by default an unlimited
4087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * number of passes are performed.  An override of the range of passes
4187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * to be executed is available through the use of the -s (start) and
4217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller * -e (end) command-line options.  Can also specify a single specific
4317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller * pass via the -p option.  There is also a default time in which the
4417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller * test executes, which is given by DEFAULT_DURATION and can be overriden
4517cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller * through the use of the -t command-line option.
4687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller */
4787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
4887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <assert.h>
4987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <errno.h>
5087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <libgen.h>
5187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <math.h>
5217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller#define _GNU_SOURCE
5387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <sched.h>
5487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <stdio.h>
5587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <stdlib.h>
5687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <time.h>
5787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <unistd.h>
5887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
5987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <sys/syscall.h>
6087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <sys/types.h>
6187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <sys/wait.h>
6287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
6387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <hardware_legacy/wifi.h>
6487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
6587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define LOG_TAG "wifiLoadScanAssocTest"
6687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <utils/Log.h>
6787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#include <testUtil.h>
6887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
6987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DEFAULT_START_PASS     0
7087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DEFAULT_END_PASS     999
7187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DEFAULT_DURATION       FLT_MAX // A fairly long time, so that
7287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                                       // range of passes will have
7387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                                       // precedence
7487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DEFAULT_DELAY_MIN      0.0     // Min delay after start supplicant
7587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DEFAULT_DELAY_MAX     20.0     // Max delay after start supplicant
7687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define DELAY_EXP            150.0     // Exponent which determines the
7787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                                       // amount by which values closer
7887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                                       // to DELAY_MIN are favored.
7987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
8087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define CMD_STATUS           "wpa_cli status 2>&1"
8187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define CMD_STOP_FRAMEWORK   "stop 2>&1"
8287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define CMD_START_FRAMEWORK  "start 2>&1"
8387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
8487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define MAXSTR      100
8587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define MAXCMD      500
8687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
8787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillertypedef unsigned int bool_t;
8887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define true (0 == 0)
8987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller#define false (!true)
9087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
9187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller// File scope variables
9287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillercpu_set_t availCPU;
9387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerunsigned int numAvailCPU;
9487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerfloat delayMin = DEFAULT_DELAY_MIN;
9587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerfloat delayMax = DEFAULT_DELAY_MAX;
9687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerbool_t driverLoadedAtStart;
9787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
9817cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller// Command-line mutual exclusion detection flags.
9917cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller// Corresponding flag set true once an option is used.
10017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemillerbool_t eFlag, sFlag, pFlag;
10117cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller
10287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller// File scope prototypes
10387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerstatic void init(void);
10487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerstatic void randDelay(void);
10587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerstatic void randBind(const cpu_set_t *availSet, int *chosenCPU);
10687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
10787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller/*
10887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Main
10987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
11087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Performs the following high-level sequence of operations:
11187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
11287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   1. Command-line parsing
11387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
11487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   2. Initialization
11587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
11687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   3. Execute passes that repeatedly perform the WiFi load, scan,
11787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *      associate, unload sequence.
11887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
11987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   4. Restore state of WiFi driver to state it was at the
12087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *      start of the test.
12187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
12287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   5. Restart framework
12387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller */
12487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerint
12587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillermain(int argc, char *argv[])
12687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller{
12787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    FILE *fp;
12887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    int rv, opt;
12987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    int cpu;
13087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    char *chptr;
13187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    unsigned int pass;
13287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    char cmd[MAXCMD];
13387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    float duration = DEFAULT_DURATION;
13487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    unsigned int startPass = DEFAULT_START_PASS, endPass = DEFAULT_END_PASS;
13587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    struct timeval startTime, currentTime, delta;
13687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
13787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testSetLogCatTag(LOG_TAG);
13887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
13987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Parse command line arguments
14017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller    while ((opt = getopt(argc, argv, "d:D:s:e:p:t:?")) != -1) {
14187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        switch (opt) {
14287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case 'd': // Minimum Delay
14387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            delayMin = strtod(optarg, &chptr);
14487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if ((*chptr != '\0') || (delayMin < 0.0)) {
14587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Invalid command-line specified minimum delay "
14687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                    "of: %s", optarg);
14787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(1);
14887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
14987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            break;
15087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
15187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case 'D': // Maximum Delay
15287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            delayMax = strtod(optarg, &chptr);
15387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if ((*chptr != '\0') || (delayMax < 0.0)) {
15487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Invalid command-line specified maximum delay "
15587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                    "of: %s", optarg);
15687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(2);
15787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
15887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            break;
15987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
16087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case 't': // Duration
16187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            duration = strtod(optarg, &chptr);
16287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if ((*chptr != '\0') || (duration < 0.0)) {
16387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Invalid command-line specified duration of: %s",
16487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                    optarg);
16587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(3);
16687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
16787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            break;
16887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
16987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case 's': // Starting Pass
17017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            if (sFlag || pFlag) {
17117cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                testPrintE("Invalid combination of command-line options,");
17217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                if (sFlag) {
17317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -s flag specified multiple times.");
17417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                } else {
17517cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -s and -p flags are mutually exclusive.");
17617cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                }
17717cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                exit(10);
17817cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            }
17917cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            sFlag = true;
18087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            startPass = strtoul(optarg, &chptr, 10);
18187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if (*chptr != '\0') {
18287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Invalid command-line specified starting pass "
18387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                    "of: %s", optarg);
18487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(4);
18587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
18687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            break;
18787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
18887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case 'e': // Ending Pass
18917cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            if (eFlag || pFlag) {
19017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                testPrintE("Invalid combination of command-line options,");
19117cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                if (sFlag) {
19217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -e flag specified multiple times.");
19317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                } else {
19417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -e and -p flags are mutually exclusive.");
19517cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                }
19617cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                exit(11);
19717cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            }
19817cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            eFlag = true;
19987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            endPass = strtoul(optarg, &chptr, 10);
20087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if (*chptr != '\0') {
20187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Invalid command-line specified ending pass "
20287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                    "of: %s", optarg);
20387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(5);
20487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
20587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            break;
20687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
20717cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller        case 'p': // Single Specific Pass
20817cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            if (pFlag || sFlag || eFlag) {
20917cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                testPrintE("Invalid combination of command-line options,");
21017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                if (pFlag) {
21117cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -p flag specified multiple times.");
21217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                } else {
21317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    testPrintE("  -p and -%c flags are mutually exclusive.",
21417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                        (sFlag) ? 's' : 'e');
21517cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                }
21617cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                exit(12);
21717cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            }
21817cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            pFlag = true;
21917cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            endPass = startPass = strtoul(optarg, &chptr, 10);
22017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            if (*chptr != '\0') {
22117cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                testPrintE("Invalid command-line specified pass "
22217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                    "of: %s", optarg);
22317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                exit(13);
22417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            }
22517cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            break;
22617cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller
22787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        case '?':
22887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        default:
22987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("  %s [options]", basename(argv[0]));
23087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("    options:");
23187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("      -s Starting pass");
23287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("      -e Ending pass");
23317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller            testPrintE("      -p Specific single pass");
23487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("      -t Duration");
23587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("      -d Delay min");
23687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("      -D Delay max");
2378c48ce6585b1823c1b5e93e049f2eab91ccb73fcLouis Huemiller            exit(((optopt == 0) || (optopt == '?')) ? 0 : 6);
23887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
23987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
24087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (delayMax < delayMin) {
24187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("Unexpected maximum delay less than minimum delay");
24287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("  delayMin: %f delayMax: %f", delayMin, delayMax);
24387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(7);
24487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
24587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (endPass < startPass) {
24687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("Unexpected ending pass before starting pass");
24787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("  startPass: %u endPass: %u", startPass, endPass);
24887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(8);
24987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
25087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (argc != optind) {
25187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("Unexpected command-line postional argument");
25287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("  %s [-s start_pass] [-e end_pass] [-d duration]",
25387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            basename(argv[0]));
25487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(9);
25587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
25687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("duration: %g", duration);
25787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("startPass: %u", startPass);
25887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("endPass: %u", endPass);
25987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("delayMin: %f", delayMin);
26087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("delayMax: %f", delayMax);
26187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
26287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    init();
26387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
26487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // For each pass
26587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    gettimeofday(&startTime, NULL);
26687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    for (pass = startPass; pass <= endPass; pass++) {
26787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Stop if duration of work has already been performed
26887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        gettimeofday(&currentTime, NULL);
26987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        delta = tvDelta(&startTime, &currentTime);
27087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if (tv2double(&delta) > duration) { break; }
27187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
2728dc3c07b1878d79fe0c39d79c5ce37ee43703d82Louis Huemiller        testPrintI("==== Starting pass: %u", pass);
27387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
27487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Use a pass dependent sequence of random numbers
27587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        srand48(pass);
27687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
27787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Load WiFi Driver
27887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        randBind(&availCPU, &cpu);
27987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_load_driver()) != 0) {
28087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("CPU: %i wifi_load_driver() failed, rv: %i\n",
28187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                cpu, rv);
28287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(20);
28387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
28487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("CPU: %i wifi_load_driver succeeded", cpu);
28587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
28687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Start Supplicant
28787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        randBind(&availCPU, &cpu);
2881799ef2df764916da22c4fdb8be20fc860e8ab03Irfan Sheriff        if ((rv = wifi_start_supplicant(false)) != 0) {
28987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("CPU: %i wifi_start_supplicant() failed, rv: %i\n",
29087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                cpu, rv);
29187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(21);
29287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
29387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("CPU: %i wifi_start_supplicant succeeded", cpu);
29487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
29587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Sleep a random amount of time
29687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        randDelay();
29787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
29887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        /*
29987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller         * Obtain WiFi Status
30087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller         * Half the time skip this step, which helps increase the
30187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller         * level of randomization.
30287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller         */
30387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if (testRandBool()) {
30487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            rv = snprintf(cmd, sizeof(cmd), "%s", CMD_STATUS);
30587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if (rv >= (signed) sizeof(cmd) - 1) {
30687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                testPrintE("Command too long for: %s\n", CMD_STATUS);
30787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                exit(22);
30887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
30987123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller            testExecCmd(cmd);
31087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
31187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
31287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Stop Supplicant
31387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        randBind(&availCPU, &cpu);
31487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_stop_supplicant()) != 0) {
31587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("CPU: %i wifi_stop_supplicant() failed, rv: %i\n",
31687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                cpu, rv);
31787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(23);
31887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
31987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("CPU: %i wifi_stop_supplicant succeeded", cpu);
32087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
32187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Unload WiFi Module
32287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        randBind(&availCPU, &cpu);
32387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_unload_driver()) != 0) {
32487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("CPU: %i wifi_unload_driver() failed, rv: %i\n",
32587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                cpu, rv);
32687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(24);
32787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
32887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("CPU: %i wifi_unload_driver succeeded", cpu);
3298dc3c07b1878d79fe0c39d79c5ce37ee43703d82Louis Huemiller
3308dc3c07b1878d79fe0c39d79c5ce37ee43703d82Louis Huemiller        testPrintI("==== Completed pass: %u", pass);
33187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
33287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
33387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // If needed restore WiFi driver to state it was in at the
33487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // start of the test.  It is assumed that it the driver
33587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // was loaded, then the wpa_supplicant was also running.
33687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (driverLoadedAtStart) {
33787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Load driver
33887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_load_driver()) != 0) {
33987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("main load driver failed, rv: %i", rv);
34087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(25);
34187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
34287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
34387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Start supplicant
3441799ef2df764916da22c4fdb8be20fc860e8ab03Irfan Sheriff        if ((rv = wifi_start_supplicant(false)) != 0) {
34587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("main start supplicant failed, rv: %i", rv);
34687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(26);
34787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
34887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
34987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Obtain WiFi Status
35087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        rv = snprintf(cmd, sizeof(cmd), "%s", CMD_STATUS);
35187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if (rv >= (signed) sizeof(cmd) - 1) {
35287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("Command too long for: %s\n", CMD_STATUS);
35387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(22);
35487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
35587123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller        testExecCmd(cmd);
35687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
35787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
35887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Start framework
35987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    rv = snprintf(cmd, sizeof(cmd), "%s", CMD_START_FRAMEWORK);
36087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (rv >= (signed) sizeof(cmd) - 1) {
36187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("Command too long for: %s\n", CMD_START_FRAMEWORK);
36287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(27);
36387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
36487123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    testExecCmd(cmd);
36587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
3668c48ce6585b1823c1b5e93e049f2eab91ccb73fcLouis Huemiller    testPrintI("Successfully completed %u passes", pass - startPass);
3678c48ce6585b1823c1b5e93e049f2eab91ccb73fcLouis Huemiller
36887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    return 0;
36987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller}
37087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
37187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller/*
37287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Initialize
37387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
37487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Perform testcase initialization, which includes:
37587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
37687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   1. Determine which CPUs are available for use
37787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
37887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   2. Determine total number of available CPUs
37987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
38087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   3. Stop framework
38187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
38287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *   4. Determine whether WiFi driver is loaded and if so
38387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *      stop wpa_supplicant and unload WiFi driver.
38487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller */
38587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillervoid
38687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerinit(void)
38787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller{
38887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    int rv;
38987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    unsigned int n1;
39087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    char cmd[MAXCMD];
39187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
39287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Use whichever CPUs are available at start of test
39387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    rv = sched_getaffinity(0, sizeof(availCPU), &availCPU);
39487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (rv != 0) {
39587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("init sched_getaffinity failed, rv: %i errno: %i",
39687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            rv, errno);
39787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(40);
39887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
39987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
40087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // How many CPUs are available
40187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    numAvailCPU = 0;
40287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    for (n1 = 0; n1 < CPU_SETSIZE; n1++) {
40387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if (CPU_ISSET(n1, &availCPU)) { numAvailCPU++; }
40487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
40587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("numAvailCPU: %u", numAvailCPU);
40687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
40787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Stop framework
40887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    rv = snprintf(cmd, sizeof(cmd), "%s", CMD_STOP_FRAMEWORK);
40987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (rv >= (signed) sizeof(cmd) - 1) {
41087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("Command too long for: %s\n", CMD_STOP_FRAMEWORK);
41187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(41);
41287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
41387123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    testExecCmd(cmd);
41487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
41587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Is WiFi driver loaded?
41687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // If so stop the wpa_supplicant and unload the driver.
41787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    driverLoadedAtStart = is_wifi_driver_loaded();
41887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("driverLoadedAtStart: %u", driverLoadedAtStart);
41987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (driverLoadedAtStart) {
42087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Stop wpa_supplicant
42187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // Might already be stopped, in which case request should
42287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        // return immediately with success.
42387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_stop_supplicant()) != 0) {
42487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("init stop supplicant failed, rv: %i", rv);
42587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(42);
42687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
42787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("Stopped wpa_supplicant");
42887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
42987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if ((rv = wifi_unload_driver()) != 0) {
43087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            testPrintE("init unload driver failed, rv: %i", rv);
43187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            exit(43);
43287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
43387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintI("WiFi driver unloaded");
43487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
43587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
43687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller}
43787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
43887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller/*
43987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Random Delay
44087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller *
44187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * Delays for a random amount of time within the range given
44287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * by the file scope variables delayMin and delayMax.  The
44387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * selected amount of delay can come from any part of the
44487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * range, with a bias towards values closer to delayMin.
44587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * The amount of bias is determined by the setting of DELAY_EXP.
44687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * The setting of DELAY_EXP should always be > 1.0, with higher
44787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * values causing a more significant bias toward the value
44887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller * of delayMin.
44987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller */
45087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillervoid randDelay(void)
45187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller{
45287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    const unsigned long nanosecspersec = 1000000000;
45387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    float            fract, biasedFract, amt;
45487123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    struct timeval   startTime, endTime;
45587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
45687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Obtain start time
45787123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    gettimeofday(&startTime, NULL);
45887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
45987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Determine random amount to sleep.
46087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Values closer to delayMin are prefered by an amount
46187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // determined by the value of DELAY_EXP.
462a31faf1b7edb5c20d3a8949ba3ca767b4f0a4a7dLouis Huemiller    fract = testRandFract();
46387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    biasedFract = pow(DELAY_EXP, fract) / pow(DELAY_EXP, 1.0);
46487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    amt = delayMin + ((delayMax - delayMin) * biasedFract);
46587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
46687123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    // Delay
46787123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    testDelay(amt);
46887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
46987123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    // Obtain end time and display delta
47087123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller    gettimeofday(&endTime, NULL);
47187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    testPrintI("delay: %.2f",
47287123a28dab0cf3e408f073c55f47c281ea329fbLouis Huemiller        (float) (tv2double(&endTime) - tv2double(&startTime)));
47387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller}
47487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
47587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemillerstatic void
47687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis HuemillerrandBind(const cpu_set_t *availSet, int *chosenCPU)
47787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller{
47887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    int rv;
47987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    cpu_set_t cpuset;
48017cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller    int chosenAvail, avail, cpu, currentCPU;
48187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
48287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Randomly bind to a CPU
48387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Lower 16 bits from random number generator thrown away,
48487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // because the low-order bits tend to have the same sequence for
48587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // different seed values.
486a31faf1b7edb5c20d3a8949ba3ca767b4f0a4a7dLouis Huemiller    chosenAvail = testRandMod(numAvailCPU);
48787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    CPU_ZERO(&cpuset);
48887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    avail = 0;
48987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
49087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        if (CPU_ISSET(cpu, availSet)) {
49187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            if (chosenAvail == avail) {
49287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                CPU_SET(cpu, &cpuset);
49387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller                break;
49487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            }
49587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            avail++;
49687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        }
49787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
49887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    assert(cpu < CPU_SETSIZE);
49987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    sched_setaffinity(0, sizeof(cpuset), &cpuset);
50087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
50187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Confirm executing on requested CPU
50217cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller    if ((currentCPU = sched_getcpu()) < 0) {
50317cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller        testPrintE("randBind sched_getcpu() failed, rv: %i errno: %i",
50417cf8983756d9a9915cc6acae99c22e12646d012Louis Huemiller                   currentCPU, errno);
50587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(80);
50687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
50787dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
50887dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    if (currentCPU != cpu) {
50987dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        testPrintE("randBind executing on unexpected CPU %i, expected %i",
51087dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller            currentCPU, cpu);
51187dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller        exit(81);
51287dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    }
51387dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller
51487dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    // Let the caller know which CPU was chosen
51587dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller    *chosenCPU = cpu;
51687dd9e92610d5e7552f5cdb6ab2578035e2210f5Louis Huemiller}
517