Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 1 | //===-- SlowOperationInformer.cpp - Keep the user informed ----------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 10 | // This file implements the SlowOperationInformer class for the LLVM debugger. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 14 | #include "llvm/Support/SlowOperationInformer.h" |
| 15 | #include "llvm/Config/config.h" // Get the signal handler return type |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 16 | #include <iostream> |
| 17 | #include <sstream> |
| 18 | #include <signal.h> |
| 19 | #include <unistd.h> |
Chris Lattner | 0a5f033 | 2003-12-31 07:31:10 +0000 | [diff] [blame] | 20 | #include <cassert> |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | /// OperationCancelled - This flag is set by the SIGINT signal handler if the |
| 24 | /// user presses CTRL-C. |
| 25 | static volatile bool OperationCancelled; |
| 26 | |
| 27 | /// ShouldShowStatus - This flag gets set if the operation takes a long time. |
| 28 | /// |
| 29 | static volatile bool ShouldShowStatus; |
| 30 | |
| 31 | /// NestedSOI - Sanity check. SlowOperationInformers cannot be nested or run in |
| 32 | /// parallel. This ensures that they never do. |
| 33 | static bool NestedSOI = false; |
| 34 | |
| 35 | static RETSIGTYPE SigIntHandler(int Sig) { |
| 36 | OperationCancelled = true; |
| 37 | signal(SIGINT, SigIntHandler); |
| 38 | } |
| 39 | |
| 40 | static RETSIGTYPE SigAlarmHandler(int Sig) { |
| 41 | ShouldShowStatus = true; |
| 42 | } |
| 43 | |
John Criswell | 2657aec | 2004-01-01 15:14:28 +0000 | [diff] [blame] | 44 | static void (*OldSigIntHandler) (int); |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | SlowOperationInformer::SlowOperationInformer(const std::string &Name) |
| 48 | : OperationName(Name), LastPrintAmount(0) { |
| 49 | assert(!NestedSOI && "SlowerOperationInformer objects cannot be nested!"); |
| 50 | NestedSOI = true; |
| 51 | |
| 52 | OperationCancelled = 0; |
| 53 | ShouldShowStatus = 0; |
| 54 | |
| 55 | signal(SIGALRM, SigAlarmHandler); |
| 56 | OldSigIntHandler = signal(SIGINT, SigIntHandler); |
| 57 | alarm(1); |
| 58 | } |
| 59 | |
| 60 | SlowOperationInformer::~SlowOperationInformer() { |
| 61 | NestedSOI = false; |
Chris Lattner | 9e26027 | 2003-12-31 10:20:38 +0000 | [diff] [blame] | 62 | if (LastPrintAmount) { |
| 63 | // If we have printed something, make _sure_ we print the 100% amount, and |
| 64 | // also print a newline. |
| 65 | std::cout << std::string(LastPrintAmount, '\b') << "Progress " |
| 66 | << OperationName << ": 100% \n"; |
| 67 | } |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 68 | |
| 69 | alarm(0); |
| 70 | signal(SIGALRM, SIG_DFL); |
| 71 | signal(SIGINT, OldSigIntHandler); |
| 72 | } |
| 73 | |
| 74 | /// progress - Clients should periodically call this method when they are in |
| 75 | /// an exception-safe state. The Amount variable should indicate how far |
| 76 | /// along the operation is, given in 1/10ths of a percent (in other words, |
| 77 | /// Amount should range from 0 to 1000). |
| 78 | void SlowOperationInformer::progress(unsigned Amount) { |
| 79 | if (OperationCancelled) { |
| 80 | std::cout << "\n"; |
| 81 | LastPrintAmount = 0; |
| 82 | throw "While " + OperationName + ", operation cancelled."; |
| 83 | } |
| 84 | |
| 85 | // If we haven't spent enough time in this operation to warrant displaying the |
| 86 | // progress bar, don't do so yet. |
| 87 | if (!ShouldShowStatus) |
| 88 | return; |
| 89 | |
| 90 | // Delete whatever we printed last time. |
| 91 | std::string ToPrint = std::string(LastPrintAmount, '\b'); |
| 92 | |
| 93 | std::ostringstream OS; |
Chris Lattner | 9e26027 | 2003-12-31 10:20:38 +0000 | [diff] [blame] | 94 | OS << "Progress " << OperationName << ": " << Amount/10; |
| 95 | if (unsigned Rem = Amount % 10) |
| 96 | OS << "." << Rem << "%"; |
| 97 | else |
| 98 | OS << "% "; |
Chris Lattner | a2e9363 | 2003-12-31 05:40:02 +0000 | [diff] [blame] | 99 | |
| 100 | LastPrintAmount = OS.str().size(); |
| 101 | std::cout << ToPrint+OS.str() << std::flush; |
| 102 | } |