Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created June 2, 2013 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vedantk/5694464 to your computer and use it in GitHub Desktop.
Save vedantk/5694464 to your computer and use it in GitHub Desktop.
Frequency count: hard mode.
#!/bin/zsh
# Find each struct's usage frequency in each subsystem. In each search, discard
# the files that have no matches, get rid of the directory prefix, only keep
# the subsystem, and then pipe the information out to a temporary file. Loop
# through every (Subsystem Of File, Count) tuple and sum up the contribution
# from each subsystem to that struct's frequency.
for line in $(cat structs.txt); do
echo ">"$line
grep -rEoIc "struct\s+""$(echo $line | cut -d : -f 2)" /mnt/ram/linux | grep -v ':0$' | sed 's|/mnt/ram/linux/||g' | sed -r "s|/(.*):|:|g" > /mnt/ram/struct
./reduce
done
#include <map>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
int fd = open("/mnt/ram/struct", O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
struct stat st;
if (fstat(fd, &st) < 0) {
perror("fstat");
return 1;
}
char *mem = (char *) mmap(0, st.st_size, PROT_READ | PROT_WRITE,
MAP_SHARED /* Don't allocate new pages when we poke in NULLs;
anyway this gets flushed to a ramfs. */
fd, 0);
if (mem == (char *) MAP_FAILED) {
perror("mmap");
return 1;
}
map<string, int> freq;
char *cur = mem;
char *end = mem + st.st_size;
while (cur < end) {
char *colon = (char *) rawmemchr(cur, ':');
*colon = '\0';
// subsystem
string key = string((const char *) cur);
cur = (char *) rawmemchr(colon + 2, '\n');
*cur = '\0';
// frequency count
int val = atoi(colon + 1);
if (freq.count(key)) {
freq[key] += val;
} else {
freq.emplace(key, val);
}
++cur;
}
for (auto it = freq.begin(); it != freq.end(); ++it) {
printf("%s:%d\n", it->first.c_str(), it->second);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment