ATLAS Offline Software
Loading...
Searching...
No Matches
AthenaBarCodeImpl.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <sstream>
6
7#include "GaudiKernel/ServiceHandle.h"
8
10
12
13#include "GaudiKernel/IMessageSvc.h"
14#include "GaudiKernel/MsgStream.h"
16
17#include <iostream>
18#include <cstdlib>
19
20#include "uuid/uuid.h"
21
22std::atomic<AthenaBarCode_t> AthenaBarCodeImpl::m_barcodeCounter = 0;
23
25
26 //This function will be call the first time any "access" function is called
27 //using const type because those "access" functions might be const
28 //Will only change mutable variables
29
30 // std::cout<<"Calling AthenaBarCodeImpl::AthenaBarCodeImpl() "<<std::endl;
31
32 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33 //Set counter part
34 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
35
36 const AthenaBarCode_t counter = m_barcodeCounter++;
37
39
40 try {
41 setBits(SCounterBits, CounterBits, counter & ((1u<<CounterBits)-1),
42 barcode);
43 }
44 catch (const GaudiException& Exception) {
45 throw std::runtime_error(
46 "AthenaBarCodeImpl::Can not Set Counter Bit, Counter Overflow");
47 }
48
49 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
50 //Set reserve part
51 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
52 try {
53 setBits(SReserveBits, ReserveBits, 0, barcode);
54 }
55 catch (const GaudiException& Exception) {
56 throw std::runtime_error(
57 "AthenaBarCodeImpl::Can not initialize Reserve Bit");
58 }
59
60 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
61 //Set Version part
62 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
63 try {
64 setBits(SVersionBits, VersionBits, 0, barcode);
65 }
66 catch (const GaudiException& Exception) {
67 throw std::runtime_error(
68 "AthenaBarCodeImpl::Can not initialize Version Bit");
69 }
70
71
72 try {
74 }
75 catch (const GaudiException& Exception) {
76 throw std::runtime_error("AthenaBarCodeImpl::Can not set UUID Hash Bits");
77 }
78
79 m_barcode = barcode;
80
81 /* std::cout << "[AthenaBarCodeImpl::m_barcode,m_defaultHash,m_barcodeCounter]= "
82 << std::hex
83 << (m_barcode)<< "\t"
84 << m_defaultHash<< "\t"
85 << (m_barcodeCounter)<<"\t"
86 << std::dec
87 << std::endl;
88 */
89}
90
95
100
102{
103 if (this != &other) {
104 m_barcode = static_cast<AthenaBarCode_t>(other.m_barcode);
105 }
106 return *this;
107}
108
109bool
112 initABC();
113
114 if (obj.getAthenaBarCode() == m_barcode)
115 return true;
116 return false;
117}
118
119bool
122 initABC();
123
124 if ((obj.getAthenaBarCode()) >> (ReserveBits + VersionBits) == m_barcode
126 return true;
127 return false;
128}
129
136
137void
139
141 initABC();
142
143 AthenaBarCodeVersion_t currversion = getVersion();
144
145 try {
147 setBits(SVersionBits, VersionBits, currversion + 1, bc);
148 m_barcode = bc;
149 }
150 catch (const GaudiException& Exception) {
151 throw std::runtime_error(
152 "AthenaBarCodeImpl::newVersion()::Version Overflow");
153 }
154}
155
156void
159 initABC();
160
161 try {
164 m_barcode = bc;
165 }
166 catch (const GaudiException& Exception) {
167 throw std::runtime_error("AthenaBarCodeImpl::newVersion()::Version Too big");
168 }
169}
170
171std::ostream&
172AthenaBarCodeImpl::dump(std::ostream& out) const {
174 initABC();
175
176 out << "\n[AthenaBarCode]= " << std::hex << (m_barcode) << std::dec << std::endl;
177 return out;
178}
179
180bool
183 initABC();
184
185 //FIXME: it is possible that m_defaultHash is not yet initialized.
186 return getUUIDHash() == getDefaultHash();
187}
188
191 const int maxdigs = 16;
192 AthenaBarCode_t tmp1 = static_cast<AthenaBarCode_t> (2166136261UL);
193
194 for (int i = 0; i < maxdigs; i++) {
195 tmp1 ^= (AthenaBarCode_t)(guid[i]);
196 tmp1 *= 16777619UL;
197 }
198
199 return (tmp1 << (TotalBits - UUIDBits)) >> (TotalBits - UUIDBits);
200}
201
209
210void
214
221
222void
225 initABC();
226
227 try {
230 m_barcode = bc;
231 }
232 catch (const GaudiException& Exception) {
233 throw std::runtime_error(
234 "AthenaBarCodeImpl::newVersion()::Version Overflow");
235 }
236}
237
240 return getUUIDHash();
241}
242
247
249AthenaBarCodeImpl::getBits(unsigned short startbit, unsigned short nbits) const {
250
251 AthenaBarCode_t tmp;
252 //now m_barcode=aaaaaxxaa;
253 tmp = m_barcode << (startbit); //tmp=xxaa00000;
254 tmp = tmp >> (TotalBits - nbits); //tmp=0000000xx
255
256 return tmp;
257}
258
259void
260AthenaBarCodeImpl::setBits(unsigned short startbit, unsigned short nbits,
262 AthenaBarCode_t& bc) const
263{
264 AthenaBarCode_t tmp;
265 AthenaBarCode_t tmp2;
266 tmp2 = 0;
267
268 if ((id) >= ((~tmp2) >> (TotalBits - nbits))) {
269
270 IMessageSvc *msgsvc = Athena::getMessageSvc();
271 if (msgsvc) {
272 MsgStream msg(msgsvc, "AthenaBarCodeImpl");
273 msg << MSG::WARNING << "setBits::bit to be set:" << id
274 << " is larger than limit:" << ((~tmp2) >> (TotalBits - nbits))
275 << endmsg;
276 }
277 else {
278 std::cout << "setBits::bit to be set:" << id << " is larger than limit:"
279 << ((~tmp2) >> (TotalBits - nbits)) << std::endl;
280 }
281 throw std::runtime_error("AthenaBarCodeImpl::setBit Failed");
282 }
283
285 tmp = (id << (TotalBits - nbits - startbit)); //tmp=00000xx00
286 tmp2 = ((~tmp2) >> (TotalBits - nbits)); //tmp2=000000011
287
288 tmp2 = (tmp2 << (TotalBits - nbits - startbit)); //tmp2=000001100
289 tmp2 = (~tmp2); //tmp2=111110011
290
291 //now m_barcode=aaaaayyaa
292 bc &= tmp2; //m_barcode=aaaaa00aa
293 bc |= tmp; //m_barcode=aaaaaxxaa
294
295}
296
297void
299 getDefaultHash (jobid);
300}
301
302
304AthenaBarCodeImpl::getDefaultHash (const char* jobid /*= nullptr*/)
305{
306 static const AthenaBarCode_t defaultHash = makeDefaultHash (jobid);
307 return defaultHash;
308}
309
310
313{
314 if (jobid) {
315 return AthenaBarCodeImpl::hashUUID(jobid);
316 }
317
318 // First look for a uuid as an environment variable.
319 // This can be set in cases where we don't have the full
320 // Gaudi/Athena environment available (eg, ARA), and we
321 // don't want to try to create JobIDSvc. This has to be
322 // communicated in some way external to this library,
323 // as we may end up here while initializing the dictionary
324 // for this library.
325 const char* env_uuid = getenv ("_ATHENABARCODEIMPL_JOBUUID");
326 if (env_uuid) {
327 return AthenaBarCodeImpl::hashUUID(env_uuid);
328 }
329
330 // std::cout<<"no UUID stored, generating."<<std::endl;
331
332 ServiceHandle<IJobIDSvc> p_jobidsvc("JobIDSvc", "JobIDSvc");
333 StatusCode sc = p_jobidsvc.retrieve();
334 if (!sc.isSuccess() || 0 == p_jobidsvc) {
335 //FIXME
336 //use uuid instead if service not available
337 JobID_t JobID;
338 uuid_generate(JobID);
339 return AthenaBarCodeImpl::hashUUID((const char *) JobID);
340 /* std::cout << "Could not find JobIDSvc, using uuid directly"
341 <<"jobid "<<o.str()
342 << std::endl;*/
343 }
344
345 PJobID_t pjobid = p_jobidsvc->getJobID();
346 return AthenaBarCodeImpl::hashUUID((const char *) pjobid);
347}
#define endmsg
uint64_t AthenaBarCode_t
barcode for all INav4Mom classes
AthenaBarCode_t AthenaBarCodeVersion_t
uuid_t JobID_t
Definition IJobIDSvc.h:20
const unsigned char * PJobID_t
Definition IJobIDSvc.h:21
static Double_t sc
static const unsigned short ReserveBits
void setBits(unsigned short startbit, unsigned short nbits, AthenaBarCode_t id, AthenaBarCode_t &bc) const
static AthenaBarCode_t makeDefaultHash(const char *jobid)
static const unsigned short SUUIDBits
void setReserveBits(AthenaBarCode_t id)
AthenaBarCode_t hasUUIDHash() const
AthenaBarCode_t getUUIDHash() const
static const unsigned short UUIDBits
static void setDefaultHash(const char *jobid)
AthenaBarCode_t getBits(unsigned short startbit, unsigned short nbits) const
std::atomic< AthenaBarCode_t > m_barcode
static const unsigned short CounterBits
static const unsigned short SCounterBits
static AthenaBarCode_t getDefaultHash(const char *jobid=nullptr)
bool hasSameAthenaBarCodeExceptVersion(const IAthenaBarCode &obj) const
AthenaBarCodeVersion_t getVersion() const
void setAthenaBarCode(AthenaBarCode_t id)
static const unsigned short SVersionBits
bool createdInCurrentJob() const
AthenaBarCode_t getAthenaBarCode() const
std::ostream & dump(std::ostream &out) const
void setVersion(AthenaBarCodeVersion_t newversion)
static const unsigned short VersionBits
static const unsigned short TotalBits
AthenaBarCodeImpl & operator=(const AthenaBarCodeImpl &)
static const unsigned short SReserveBits
static AthenaBarCode_t hashUUID(const char *)
AthenaBarCode_t getReserveBits() const
static std::atomic< AthenaBarCode_t > m_barcodeCounter
bool hasSameAthenaBarCode(const IAthenaBarCode &obj) const
static const AthenaBarCode_t UNDEFINEDBARCODE
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
MsgStream & msg
Definition testRead.cxx:32