-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use fastcrc if available #943
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
parse a tlog, for performance testing | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#define MAVLINK_USE_CONVENIENCE_FUNCTIONS | ||
#define MAVLINK_USE_MESSAGE_INFO | ||
#define MAVLINK_COMM_NUM_BUFFERS 2 | ||
|
||
// this trick allows us to make mavlink_message_t as small as possible | ||
// for this dialect, which saves some memory | ||
#include <version.h> | ||
#define MAVLINK_MAX_PAYLOAD_LEN MAVLINK_MAX_DIALECT_PAYLOAD_SIZE | ||
|
||
#include <mavlink_types.h> | ||
static mavlink_system_t mavlink_system = {42,11,}; | ||
|
||
#define MAVLINK_ASSERT(x) assert(x) | ||
static void comm_send_ch(mavlink_channel_t chan, uint8_t c) {} | ||
|
||
#include <mavlink.h> | ||
|
||
int main(int argc, const char *argv[]) | ||
{ | ||
if (argc < 2) { | ||
printf("Usage: parse_tlog FILENAME\n"); | ||
exit(1); | ||
} | ||
const char *filename = argv[1]; | ||
int fd = open(filename, O_RDONLY); | ||
if (fd == -1) { | ||
perror(filename); | ||
exit(1); | ||
} | ||
mavlink_channel_t chan = MAVLINK_COMM_0; | ||
char c; | ||
uint32_t msg_count = 0; | ||
mavlink_message_t last_msg; | ||
mavlink_status_t status; | ||
|
||
while (read(fd, &c, 1) == 1) { | ||
if (mavlink_parse_char(chan, c, &last_msg, &status)) { | ||
msg_count++; | ||
} | ||
} | ||
printf("parsed %u messages\n", (unsigned)msg_count); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,13 @@ | |
import sys | ||
from builtins import object | ||
|
||
try: | ||
import fastcrc | ||
mcrf4xx = fastcrc.crc16.mcrf4xx | ||
except Exception: | ||
mcrf4xx = None | ||
|
||
class x25crc(object): | ||
class x25crc_slow(object): | ||
"""CRC-16/MCRF4XX - based on checksum.h from mavlink library""" | ||
|
||
def __init__(self, buf=None): | ||
|
@@ -39,3 +44,57 @@ def accumulate_str(self, buf): | |
Provided for backwards compatibility. accumulate now also works on strings. | ||
""" | ||
return self.accumulate(buf) | ||
|
||
class x25crc_fast(object): | ||
"""CRC-16/MCRF4XX - based on checksum.h from mavlink library""" | ||
def __init__(self, buf=None): | ||
self.crc = 0xFFFF | ||
if buf is not None: | ||
self.accumulate(buf) | ||
|
||
def accumulate(self, buf): | ||
"""add in some more bytes (it also accepts strings)""" | ||
if sys.version_info[0] == 2: | ||
if type(buf) is str: | ||
buf = bytearray(buf) | ||
elif type(buf).__name__ == 'unicode': | ||
# we can't use the identifier unicode in python3 | ||
buf = bytearray(buf.encode()) | ||
elif type(buf) is str: | ||
buf = bytes(buf.encode()) | ||
elif type(buf) is list: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should all of these be |
||
buf = bytes(buf) | ||
self.crc = mcrf4xx(buf, self.crc) | ||
|
||
def accumulate_str(self, buf): | ||
""" | ||
Provided for backwards compatibility. accumulate now also works on strings. | ||
""" | ||
return self.accumulate(buf) | ||
|
||
if mcrf4xx is not None: | ||
x25crc = x25crc_fast | ||
else: | ||
x25crc = x25crc_slow | ||
|
||
if __name__ == "__main__": | ||
import random, time | ||
|
||
for i in range(100): | ||
nbytes = random.randint(1000,5000) | ||
b = random.randbytes(nbytes) | ||
|
||
t1 = time.time() | ||
slow = x25crc_slow() | ||
t2 = time.time() | ||
slow_t = t2 - t1 | ||
|
||
t1 = time.time() | ||
c = x25crc() | ||
t2 = time.time() | ||
fast_t = t2 - t1 | ||
|
||
slow.accumulate(b) | ||
c.accumulate(b) | ||
assert slow.crc == c.crc | ||
print("Speedup %.2f" % (slow_t/fast_t)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we really be adding Py2 compat here?