blob: 132cc00c813a50df1a8567852c79f144c11dd5e6 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2012 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#if WITH_LIB_CONSOLE
24
25#include <ctype.h>
26#include <debug.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <kernel/thread.h>
30#include <platform.h>
31#include <lib/cksum.h>
32
33#include <lib/console.h>
34
35static int cmd_crc16(int argc, const cmd_args *argv);
36static int cmd_crc32(int argc, const cmd_args *argv);
37static int cmd_adler32(int argc, const cmd_args *argv);
38static int cmd_cksum_bench(int argc, const cmd_args *argv);
39
40STATIC_COMMAND_START
41#if LK_DEBUGLEVEL > 0
42 STATIC_COMMAND("crc16", "crc16", &cmd_crc16)
43 STATIC_COMMAND("crc32", "crc32", &cmd_crc32)
44 STATIC_COMMAND("adler32", "adler32", &cmd_adler32)
45#endif
46#if LK_DEBUGLEVEL > 1
47 STATIC_COMMAND("bench_cksum", "benchmark the checksum routines", &cmd_cksum_bench)
48#endif
49STATIC_COMMAND_END(crc);
50
51static int cmd_crc16(int argc, const cmd_args *argv)
52{
53 if (argc < 3) {
54 printf("not enough arguments\n");
55 printf("usage: %s <address> <size>\n", argv[0].str);
56 return -1;
57 }
58
59 uint16_t crc = crc16(argv[1].p, argv[2].u);
60
61 printf("0x%hx\n", crc);
62
63 return 0;
64}
65
66static int cmd_crc32(int argc, const cmd_args *argv)
67{
68 if (argc < 3) {
69 printf("not enough arguments\n");
70 printf("usage: %s <address> <size>\n", argv[0].str);
71 return -1;
72 }
73
74 uint32_t crc = crc32(0, argv[1].p, argv[2].u);
75
76 printf("0x%x\n", crc);
77
78 return 0;
79}
80
81static int cmd_adler32(int argc, const cmd_args *argv)
82{
83 if (argc < 3) {
84 printf("not enough arguments\n");
85 printf("usage: %s <address> <size>\n", argv[0].str);
86 return -1;
87 }
88
89 uint32_t crc = adler32(0, argv[1].p, argv[2].u);
90
91 printf("0x%x\n", crc);
92
93 return 0;
94}
95
96static int cmd_cksum_bench(int argc, const cmd_args *argv)
97{
98#define BUFSIZE 0x1000
99#define ITER 16384
100 void *buf;
101 bool freebuf;
102
103 if (argc > 1) {
104 buf = argv[1].p;
105 freebuf = false;
106 } else {
107 buf = malloc(BUFSIZE);
108 freebuf = true;
109 }
110
111 if (!buf)
112 return -1;
113
114 lk_bigtime_t t;
115 uint32_t crc;
116
117 printf("buffer at %p, size %u\n", buf, BUFSIZE);
118
119 t = current_time_hires();
120 crc = 0;
121 for (int i = 0; i < ITER; i++) {
122 crc = crc32(crc, buf, BUFSIZE);
123 }
124 t = current_time_hires() - t;
125
126 printf("took %llu usecs to crc32 %d bytes (%lld bytes/sec)\n", t, BUFSIZE * ITER, (BUFSIZE * ITER) * 1000000ULL / t);
127 thread_sleep(500);
128
129 t = current_time_hires();
130 crc = 0;
131 for (int i = 0; i < ITER; i++) {
132 crc = adler32(crc, buf, BUFSIZE);
133 }
134 t = current_time_hires() - t;
135
136 printf("took %llu usecs to adler32 %d bytes (%lld bytes/sec)\n", t, BUFSIZE * ITER, (BUFSIZE * ITER) * 1000000ULL / t);
137
138 if (freebuf)
139 free(buf);
140 return 0;
141}
142
143#endif // WITH_LIB_CONSOLE
144
145// vim: set noexpandtab: