monotone

monotone Mtn Source Tree

Root/sha1.cc

1// Copyright (C) 2006 Nathaniel Smith <njs@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10// This file holds a registry of different SHA-1 implementations, and lets us
11// benchmark them.
12
13#include "base.hh"
14#include <map>
15#include <botan/engine.h>
16#include <botan/libstate.h>
17
18#include "sha1.hh"
19#include "sha1_engine.hh"
20#include "safe_map.hh"
21#include "sanity.hh"
22#include "ui.hh"
23#include "platform.hh"
24#include "cmd.hh"
25#include "transforms.hh"
26
27using std::map;
28using std::pair;
29using std::make_pair;
30using std::string;
31
32namespace
33{
34 map<int, pair<string, sha1_maker *> > & registry()
35 {
36 static map<int, pair<string, sha1_maker *> > the_registry;
37 return the_registry;
38 }
39
40 void
41 register_sha1(int priority, std::string const & name, sha1_maker * maker)
42 {
43 // invert priority, so that high priority sorts first (could override the
44 // comparison function too, but this takes 1 character...)
45 safe_insert(registry(), make_pair(-priority, make_pair(name, maker)));
46 }
47
48 sha1_maker * maker_to_be_benchmarked = 0;
49
50 class Monotone_SHA1_Engine : public Botan::Engine
51 {
52 public:
53 Botan::HashFunction * find_hash(const std::string& name) const
54 {
55 if (name == "SHA-160")
56 {
57 if (maker_to_be_benchmarked)
58 {
59 // We are in the middle of a benchmark run, so call the maker we
60 // are supposed to be benchmarking.
61 Botan::HashFunction * retval = maker_to_be_benchmarked();
62 maker_to_be_benchmarked = 0;
63 return retval;
64 }
65 else
66 {
67 I(!registry().empty());
68 // Call the highest priority maker.
69 return registry().begin()->second.second();
70 }
71 }
72 return 0;
73 }
74 };
75
76 // returning 0 from find_hash means that we don't want to handle this, and
77 // causes Botan to drop through to its built-in, portable engine.
78 Botan::HashFunction * botan_default_maker()
79 {
80 return 0;
81 }
82 sha1_registerer botan_default(0, "botan", &botan_default_maker);
83}
84
85sha1_registerer::sha1_registerer(int priority, string const & name, sha1_maker * maker)
86{
87 register_sha1(priority, name, maker);
88}
89
90void hook_botan_sha1()
91{
92 Botan::global_state().add_engine(new Monotone_SHA1_Engine);
93}
94
95CMD_HIDDEN(benchmark_sha1, "benchmark_sha1", "", CMD_REF(debug), "",
96 N_("Benchmarks SHA-1 cores"),
97 "",
98 options::opts::none)
99{
100 P(F("Benchmarking %s SHA-1 cores") % registry().size());
101 int mebibytes = 100;
102 string test_str(mebibytes << 20, 'a');
103 data test_data(test_str);
104 for (map<int, pair<string, sha1_maker*> >::const_iterator i = registry().begin();
105 i != registry().end(); ++i)
106 {
107 maker_to_be_benchmarked = i->second.second;
108 id foo;
109 double start = cpu_now();
110 calculate_ident(test_data, foo);
111 double end = cpu_now();
112 double mebibytes_per_sec = mebibytes / (end - start);
113 P(F("%s: %s MiB/s") % i->second.first % mebibytes_per_sec);
114 }
115}
116
117
118// Local Variables:
119// mode: C++
120// fill-column: 76
121// c-file-style: "gnu"
122// indent-tabs-mode: nil
123// End:
124// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
125

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status