1
1
/*
2
- * Partial Copyright (C) 2019 Xilinx, Inc
2
+ * Partial Copyright (C) 2019-2020 Xilinx, Inc
3
3
*
4
4
* Microsoft provides sample code how RESTful APIs are being called
5
5
*
25
25
#include < syslog.h>
26
26
#include < openssl/sha.h>
27
27
#include < curl/curl.h>
28
+ #include < sys/time.h>
28
29
29
30
#include < cstdio>
30
31
#include < cstring>
@@ -229,6 +230,9 @@ int AzureDev::azureLoadXclBin(const xclBin *buffer)
229
230
index++;
230
231
}
231
232
233
+ timeval tim_s, tim_e;
234
+ gettimeofday (&tim_s, NULL );
235
+ std::cout << fpgaSerialNumber << " : upload xclbin start @" << tim_s.tv_sec << std::endl;
232
236
// start the re-image process
233
237
std::string delim = " :" ;
234
238
std::string ret, key, value;
@@ -242,6 +246,10 @@ int AzureDev::azureLoadXclBin(const xclBin *buffer)
242
246
value.compare (" 0" ) != 0 )
243
247
return -EFAULT;
244
248
249
+ gettimeofday (&tim_e, NULL );
250
+ std::cout << fpgaSerialNumber << " : upload xclbin end @" << tim_e.tv_sec ;
251
+ std::cout << " ,takes " << tim_e.tv_sec - tim_s.tv_sec << " seconds" << std::endl;
252
+ tim_s = tim_e;
245
253
// check the re-image status
246
254
int wait = 0 ;
247
255
do {
@@ -251,18 +259,22 @@ int AzureDev::azureLoadXclBin(const xclBin *buffer)
251
259
fpgaSerialNumber
252
260
);
253
261
if (splitLine (ret, key, value, delim) != 0 ||
254
- key.compare (" GetReimagingStatus" ) != 0 )
255
- return -EFAULT;
256
-
257
- if (value.compare (" 3" ) != 0 ) {
262
+ key.compare (" GetReimagingStatus" ) != 0 ) {
263
+ std::cout << " Retrying GetReimagingStatus ... " << std::endl;
264
+ sleep (1 );
265
+ wait++;
266
+ continue ;
267
+ } else if (value.compare (" 3" ) != 0 ) {
258
268
sleep (1 );
259
269
wait++;
260
270
continue ;
261
271
} else {
262
- std::cout << " reimaging return status: " << value << " within " << wait << " s" << std::endl;
272
+ gettimeofday (&tim_e, NULL );
273
+ std::cout << fpgaSerialNumber << " : reimage(return status: " << value << " ) end @" << tim_e.tv_sec ;
274
+ std::cout << " ,takes " << tim_e.tv_sec - tim_s.tv_sec << " seconds" << std::endl;
263
275
return 0 ;
264
276
}
265
- } while (wait < REIMAGE_TIMEOUT );
277
+ } while (wait < rest_timeout );
266
278
267
279
return -ETIMEDOUT;
268
280
}
@@ -311,7 +323,7 @@ int AzureDev::azureHotReset()
311
323
std::cout << " getreset status return status: " << value << " within " << wait << " s" << std::endl;
312
324
return 0 ;
313
325
}
314
- } while (wait < REIMAGE_TIMEOUT );
326
+ } while (wait < rest_timeout );
315
327
syslog (LOG_INFO, " complete get reset status" );
316
328
return 0 ;
317
329
}
@@ -339,6 +351,8 @@ int AzureDev::UploadToWireServer(
339
351
CURL *curl;
340
352
CURLcode res;
341
353
struct write_unit unit;
354
+ int retryCounter = 0 ;
355
+ long responseCode = 0 ;
342
356
343
357
unit.uptr = data.c_str ();
344
358
unit.sizeleft = data.size ();
@@ -377,12 +391,42 @@ int AzureDev::UploadToWireServer(
377
391
curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
378
392
379
393
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
380
- res = curl_easy_perform (curl);
381
394
382
- if (res != CURLE_OK) {
383
- std::cerr << " curl_easy_perform() failed: " << curl_easy_strerror (res) << std::endl;
384
- return 1 ;
385
- }
395
+ // add retry for http requests
396
+ do {
397
+ responseCode = 0 ;
398
+ res = curl_easy_perform (curl);
399
+
400
+ if (res != CURLE_OK) {
401
+ std::cerr << " curl_easy_perform() failed: " << curl_easy_strerror (res) << std::endl;
402
+ if (retryCounter < upload_retry) {
403
+ retryCounter++;
404
+ std::cout << " Retrying an upload..." << retryCounter << std::endl;
405
+ sleep (1 );
406
+ } else {
407
+ std::cerr << " Max number of retries reached... givin up" << std::endl;
408
+ curl_easy_cleanup (curl);
409
+ return 1 ;
410
+ }
411
+ } else {
412
+ // check the return code
413
+ curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &responseCode);
414
+ std::cout << " Debug: status code " << responseCode << std::endl;
415
+ if (responseCode >= 400 ) { // retry for networking issue
416
+ // error range
417
+ res = CURLE_HTTP_RETURNED_ERROR;
418
+ if (retryCounter < upload_retry) {
419
+ retryCounter++;
420
+ std::cout << " Retrying an upload after http error..." << retryCounter << std::endl;
421
+ sleep (1 );
422
+ } else {
423
+ std::cerr << " Max number of retries reached... givin up" << std::endl;
424
+ curl_easy_cleanup (curl);
425
+ return 1 ;
426
+ }
427
+ } // if (responseCode > 400)
428
+ } // if (res != CURLE_OK)
429
+ } while (res != CURLE_OK);
386
430
387
431
// cleanup
388
432
curl_easy_cleanup (curl);
@@ -403,7 +447,7 @@ std::string AzureDev::REST_Get(
403
447
std::string readbuff = " " ;
404
448
405
449
curl = curl_easy_init ();
406
- if (curl)
450
+ if (curl)
407
451
{
408
452
std::stringstream urlStream;
409
453
urlStream << " http://" << ip << " /" << endpoint << " &chipid=" << target;
@@ -445,15 +489,15 @@ int AzureDev::Sha256AndSplit(
445
489
446
490
while (pos < input.size ())
447
491
{
448
- std::string segment = input.substr (pos, TRANSFER_SEGMENT_SIZE );
492
+ std::string segment = input.substr (pos, transfer_segment_size );
449
493
450
494
if (!SHA256_Update (&context, segment.c_str (), segment.size ()))
451
495
{
452
496
std::cerr << " Unable to Update SHA256 buffer" << std::endl;
453
497
return 1 ;
454
498
}
455
499
output.push_back (segment);
456
- pos += TRANSFER_SEGMENT_SIZE ;
500
+ pos += transfer_segment_size ;
457
501
}
458
502
459
503
// Get Final SHA
0 commit comments