File: uniquepid.cpp

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (210 lines) | stat: -rw-r--r-- 5,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/****************************************************************************
 *   Copyright (C) 2006-2013 by Jason Ansel, Kapil Arya, and Gene Cooperman *
 *   jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu              *
 *                                                                          *
 *  This file is part of DMTCP.                                             *
 *                                                                          *
 *  DMTCP is free software: you can redistribute it and/or                  *
 *  modify it under the terms of the GNU Lesser General Public License as   *
 *  published by the Free Software Foundation, either version 3 of the      *
 *  License, or (at your option) any later version.                         *
 *                                                                          *
 *  DMTCP is distributed in the hope that it will be useful,                *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU Lesser General Public License for more details.                     *
 *                                                                          *
 *  You should have received a copy of the GNU Lesser General Public        *
 *  License along with DMTCP:dmtcp/src.  If not, see                        *
 *  <http://www.gnu.org/licenses/>.                                         *
 ****************************************************************************/

#include <stdlib.h>
#include <time.h>
#include "uniquepid.h"
#include "constants.h"
#include "../jalib/jconvert.h"
#include "../jalib/jfilesystem.h"
#include "../jalib/jserialize.h"
#include "syscallwrappers.h"
#include "protectedfds.h"
#include "shareddata.h"

using namespace dmtcp;

void dmtcp_UniquePid_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data)
{
  switch (event) {
    case DMTCP_EVENT_RESTART:
      break;

    default:
      break;
  }
}

inline static long theUniqueHostId()
{
#ifdef USE_GETHOSTID
  return ::gethostid()
#else
  //gethostid() calls socket() on some systems, which we don't want
  char buf[512];
  JASSERT(::gethostname(buf, sizeof(buf))==0)(JASSERT_ERRNO);
  //so return a bad hash of our hostname
  long h = 0;
  for(char* i=buf; *i!='\0'; ++i)
    h = (*i) + (331*h);
  //make it positive for good measure
  return h>0 ? h : -1*h;
#endif
}

static UniquePid& nullProcess()
{
  static char buf[sizeof(UniquePid)];
  static UniquePid* t=NULL;
  if(t==NULL) t = new (buf) UniquePid(0,0,0);
  return *t;
}
static UniquePid& theProcess()
{
  static char buf[sizeof(UniquePid)];
  static UniquePid* t=NULL;
  if(t==NULL) t = new (buf) UniquePid(0,0,0);
  return *t;
}
static UniquePid& parentProcess()
{
  static char buf[sizeof(UniquePid)];
  static UniquePid* t=NULL;
  if(t==NULL) t = new (buf) UniquePid(0,0,0);
  return *t;
}

// _computation_generation field of return value may later have to be modified.
// So, it can't return a const UniquePid
UniquePid& UniquePid::ThisProcess(bool disableJTrace /*=false*/)
{
  struct timespec value;
  uint64_t nsecs = 0;
  if ( theProcess() == nullProcess() )
  {
    JASSERT(clock_gettime(CLOCK_MONOTONIC, &value) == 0);
    nsecs = value.tv_sec*1000000000L + value.tv_nsec;
    theProcess() = UniquePid ( theUniqueHostId() ,
                               ::getpid(),
                               nsecs );
    if (disableJTrace == false) {
      JLOG(DMTCP) ( "recalculated process UniquePid..." ) ( theProcess() );
    }
  }

  return theProcess();
}

UniquePid& UniquePid::ParentProcess()
{
  return parentProcess();
}

/*!
    \fn UniquePid::UniquePid()
 */
UniquePid::UniquePid()
{
  _pid = 0;
  _hostid = 0;
  memset(&_time, 0, sizeof(_time));
}

// This is called only by the DMTCP coordinator.
void  UniquePid::incrementGeneration()
{
  _computation_generation++;
}

/*!
    \fn UniquePid::operator<() const
 */
bool UniquePid::operator< ( const UniquePid& that ) const
{
#define TRY_LEQ(param) if(this->param != that.param) return this->param < that.param;
  TRY_LEQ ( _hostid );
  TRY_LEQ ( _pid );
  TRY_LEQ ( _time );
  return false;
}

bool UniquePid::operator== ( const UniquePid& that ) const
{
  return _hostid==that.hostid()
         && _pid==that.pid()
         && _time==that.time();
}

ostream& dmtcp::operator<< ( dmtcp::ostream& o,const UniquePid& id )
{
  o << std::hex << id.hostid() << '-' << std::dec << id.pid() << '-' << std::hex << id.time() << std::dec;
  return o;
}

ostream& dmtcp::operator<< ( dmtcp::ostream& o,const DmtcpUniqueProcessId& id )
{
  o << std::hex << id._hostid<< '-' << std::dec << id._pid << '-' << std::hex << id._time << std::dec;
  return o;
}

bool dmtcp::operator==(const DmtcpUniqueProcessId& a,
                       const DmtcpUniqueProcessId& b)
{
  return a._hostid == b._hostid &&
         a._pid == b._pid &&
         a._time == b._time &&
         a._computation_generation == b._computation_generation;
}

bool dmtcp::operator!=(const DmtcpUniqueProcessId& a,
                       const DmtcpUniqueProcessId& b)
{
  return !(a == b);
}

string UniquePid::toString() const{
  ostringstream o;
  o << *this;
  return o.str();
}

void UniquePid::resetOnFork ( const UniquePid& newId )
{
  // parentProcess() is for inspection tools
  parentProcess() = ThisProcess();
  JLOG(DMTCP) ( "Explicitly setting process UniquePid" ) ( newId );
  theProcess() = newId;
}

bool UniquePid::isNull() const
{
  return (*this == nullProcess());
}

void UniquePid::serialize ( jalib::JBinarySerializer& o )
{
  // NOTE: Do not put JTRACE/JLOG/JNOTE/JASSERT in here
  UniquePid theCurrentProcess, theParentProcess;

  if ( o.isWriter() )
  {
    theCurrentProcess = ThisProcess();
    theParentProcess = ParentProcess();
  }

  o & theCurrentProcess & theParentProcess;

  if ( o.isReader() )
  {
    theProcess() = theCurrentProcess;
    parentProcess() = theParentProcess;
  }
}